Shifting Paths (Large)
Time limit30sMemory limit512 MB
Count how many paths the alternating left-right walk takes from clearing 1 to clearing N, or report Infinity if it never arrives.
- Level
Medium4 of 10
- Topics
- Simulation, Graph
- Solved
- No attempts yet
Problem
You have been walking in the woods for hours, and now you want to go home.
The woods contain clearings numbered . You are standing in clearing 1, and you must reach clearing to leave the woods. Each clearing from 1 to has one left path and one right path leading out to other clearings, and it may also have several one way paths leading in. The woods are haunted, so every time you enter a clearing, shifting trees block one of its two outgoing paths. On your -th visit to a single clearing the rule is this.
- You must leave along the left path if is odd.
- You must leave along the right path if is even.
- Every path is one way, so you have no choice at any step. You must go forward through the one path that is not blocked.
So the first time you stand in clearing 1 you leave along its left path. If you come back to clearing 1 a second time you leave along its right path, and on the third visit you leave along the left path again.
You begin in clearing 1, and you leave the woods once you get to clearing . How many paths do you follow before you get out?
Input
The first line contains the number of test cases . test cases follow, each beginning with a line that contains one integer .
The next lines each contain two integers and . is the clearing you end up at if you follow the left path out of clearing , and is the clearing you end up at if you follow the right path out of clearing .
No paths are given for clearing , because you are finished once you get there.
Output
For each test case, print one line containing "Case #x: y", where is the test case number starting from 1 and is the number of paths you follow to get to clearing . If you will never get to clearing , print "Infinity" in place of .
Constraints
- for every
- If you can leave the woods, the number of paths you follow is at most .
Hint
In the first test case of the first example input, your route through the woods is the one below.