The Game of Death
InterviewTime limit1sMemory limit256 MB
Starting from player 1, follow the pointed-to players and report the first step that reaches player N, or 0 when it never does.
- Level
Easy3 of 10
- Topics
- Graph, Simulation
- Solved
- No attempts yet
Problem
Heehyun and Jookyung like the game of death.
The rules are these.
- Every player points at exactly one player. Pointing at yourself is allowed.
- The player who starts the game says a positive integer .
- The player that the starter points at is the first player pointed at, the player that this player points at is the second one, and so on. The -th player pointed at loses.
Heehyun starts the game and wants Jookyung to lose. Find the number Heehyun has to say.
Input
The first line has the number of test cases ().
The first line of each test case has the number of players (). Each of the next lines has one integer, the -th of them being (), the player that player points at.
Heehyun is player 1 and Jookyung is player . When is 1, Heehyun and Jookyung are the same person.
Output
For each test case, print on one line the smallest that makes Jookyung lose.
If no number makes Jookyung lose, print 0.