We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Clues on a Binary Path

Clues on a Binary Path

Problem
Submissions
Leaderboard
Discussions
Editorial

Logan and Veronica live in Neptune, which has houses and bidirectional roads connecting them. Each road has an assigned value, , where , and each house is numbered with a distinct integer from to .

Logan and Veronica are looking for clues and need to find the number of different paths of length from house number . Each path is characterized by a binary sequence of length , where each integer in the path is the value of for the edge in the path. Two paths are different if the binary sequences characterizing these paths are distinct. Note that they may need to visit the same house several times or use the same road several times to find all possible paths.

Given a map of Neptune, help Logan and Veronica find and print the number of different paths of length from house number to the other houses in Neptune.

Input Format

The first line contains three space-separated integers describing the respective values of (the number of houses), (the number of bidirectional roads), and (the distance they want to travel).
Each of the subsequent lines contains three space-separated integers describing the respective values of , , and that define a bidirectional road between houses and having assigned value .

Constraints

  • There may be roads connecting house to itself.
  • There may be more than one road between two houses.

Output Format

Print an integer denoting the total number of paths.

Sample Input

3 2 3
1 2 0
1 3 1

Sample Output

4

Explanation

There are four possible paths:

Thus, we print as our answer.

Author

YuryBandarchuk

Difficulty

Hard

Max Score

60

Submitted By

908

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy