Waterpark
InterviewTime limit1sMemory limit128 MB
Count the number of distinct paths from point 1 to point n in a DAG where every edge goes from a lower to a higher numbered point.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Graph
- Solved
- No attempts yet
Problem
The local waterpark has a great slide complex, with many paths crisscrossing down the hill. There is one start point and one end point, but at various points you can turn and take different paths. Walter and Wanda wonder exactly how many different ways there are to go down the slide. Can you solve their problem?
More precisely, there are marked points (including the start at point and the end at point ) where the paths down the hill may split or merge. All paths move down the hill toward higher-numbered points; some paths cross over others without meeting, but we do not have to worry about that, nor about collisions between sliders. The task is simply to determine the number of different sequences of marked points one can follow down the hill.
For example, at one small waterpark there are points, with direct slides from to points and ; from to and ; and from to . There are ways down the hill: you can go , , or .
Hint: think about starting from the bottom of the slide.
Input
The first line contains a single integer (), the number of marked points. Each of the following lines contains a point pair of the form x y, where , indicating a direct slide from point to point . For example, 1234 8765 indicates a direct slide from point to point . The list ends with the point pair 0 0.
Output
Print a single integer: the number of different paths from point to point . You may assume this number is less than . If there is no path from point to point , the number of paths is .