#P7830. Dark Maze
Dark Maze
Dark Maze
The dark maze is a tree with \(n\) rooms and \(n-1\) corridors. The rooms are numbered \(1, 2, \dots, n\). In this entirely dark maze, you cannot tell where you are. To orient yourself, each room has a laser indicator that initially points to one of the corridors connected to that room.
You will repeatedly perform the following strategy:
- Rotate the laser indicator in your current room in the clockwise direction to point to the next corridor. (If the room has \(d\) corridors and the current pointer is at index \(i\) (0-indexed), then it will be updated to \((i+1) \bmod d\).)
- Move to the room at the other end of the corridor now pointed to by the laser.
You start in room \(1\) and perform the strategy for a total of \(k\) moves. You will be given \(q\) queries. In each query, the maze is reset to its initial state (that is, each room's laser indicator is reset to its initial corridor) and you are asked to determine the room you will reach after \(k\) moves.
Input Format Details: Each room supplies the list of its adjacent rooms in clockwise order, with the first neighbor being the one initially pointed to by the laser indicator.
inputFormat
The first line contains two integers \(n\) and \(q\) --- the number of rooms and the number of queries.
Then follow \(n\) lines. The \(i\)-th of these lines describes room \(i\) and starts with an integer \(d_i\) denoting the number of corridors connected to room \(i\), followed by \(d_i\) integers. These \(d_i\) integers represent the rooms connected to room \(i\) in clockwise order. The first corridor in this list is the corridor to which the laser indicator initially points.
After that, there are \(q\) lines. Each of these lines contains an integer \(k\) --- the number of moves to perform starting from room \(1\>.
outputFormat
For each query, output the room number you will reach after \(k\) moves. Each answer should be on its own line.
sample
4 3
2 2 3
2 1 4
1 1
1 2
1
2
4
3
1
4
</p>