#P13336. [GCJ 2012 Finals] Shifting Paths

    ID: 15202 远端评测题 6000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2012折半搜索 meet in the middleGoogle Code Jam

[GCJ 2012 Finals] Shifting Paths

题目描述

You have been walking in the woods for hours, and you want to go home.

The woods contain NN clearings labeled 1,2,,N1, 2, \dots, N. You are now at clearing 11, and you must reach clearing NN in order to leave the woods. Each clearing from 11 to N1N-1 has a left path and a right path leading out to other clearings, as well as some number of one-way paths leading in. Unfortunately, the woods are haunted, and any time you enter a clearing, one of the two outgoing paths will be blocked by shifty trees. More precisely, on your kthk^{th} visit to any single clearing:

  • You must leave along the left path if kk is odd.
  • You must leave along the right path if kk is even.
  • All paths are one-way, so you have no choice at each step: you must go forward through the one unblocked outgoing path.

So the first time you are in clearing #1, you will leave along the left path. If you ever come back to clearing #1 for a second time, you would leave along the right path; the third time, you'd leave along the left path again; and so on.

You begin at clearing #1, and when you get to clearing #N, you can leave the woods. How many paths do you need to follow before you get out?

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow, each beginning with a line containing a single integer NN.

N1N-1 lines follow, each containing two integers LiL_i and RiR_i. Here, LiL_i represents the clearing you would end up at if you follow the left path out of clearing ii, and RiR_i represents the clearing you would end up at if you follow the right path out of clearing ii.

No paths are specified for clearing NN because once you get there, you are finished.

输出格式

For each test case, output one line containing "Case #xx: yy", where xx is the case number (starting from 1) and yy is the number of paths you need to follow to get to clearing NN. If you will never get to clearing NN, output "Infinity" instead.

2
4
2 1
3 1
2 4
3
2 2
1 2
Case #1: 8
Case #2: Infinity

提示

Sample Explanation

In the first sample case, your route through the woods will be as shown below:

Paths followed Clearing Path direction
0 1 Left
1 2
2 3
3 2 Right
4 1
5 Left
6 2
7 3 Right
8 4 -

Limits

  • 1T30.1 \leq T \leq 30.
  • 1Li,RiN1 \leq L_i, R_i \leq N for all ii.

Test set 1 (5 Pts, Visible Verdict)

  • 2N10.2 \leq N \leq 10.

Test set 2 (46 Pts, Hidden Verdict)

  • 2N40.2 \leq N \leq 40.