Through Another Maze Darkly
Time limit8sMemory limit512 MB
Given a tree maze where each room has a rotating laser pointer, find the room reached from room 1 after exactly K corridor moves for each of Q queries.
- Level
Hard9 of 10
- Topics
- Tree, Graph, Simulation
- Solved
- No attempts yet
Problem
There is a maze formed by connecting rooms with corridors. The rooms are numbered 1 to , and each room is shaped like a circle. The corridors follow these rules:
- Each corridor connects two distinct rooms.
- Every pair of rooms is connected by exactly one path of corridors.
The lights are all out, so you cannot see where you are, which makes the maze hard to navigate. To help, each room contains a laser pointer that initially points to a corridor. Consider this strategy:
- Rotate the room's laser pointer clockwise until it points to another corridor.
- Follow the room's laser pointer and traverse the corridor.
- Repeat the previous two steps indefinitely.
You created queries to investigate this strategy. For each query, you are given an integer , and you start at room 1. Determine the final room after traversing exactly corridors. All laser pointers reset to their original orientation after each query.
Input
The first line contains the integers and (, ).
The next lines describe the layout of the maze. Line describes room . It contains an integer , the number of corridors connected to room , followed by integers , the rooms these corridors lead to, in clockwise order. Room 's laser pointer initially points to the corridor leading to room .
The final lines each contain an integer ().
Output
Output lines, answering the queries in order.
Hint
This is the initial state of the maze.

The strategy visits these rooms in order: 1, 2, 1, 2, 4, 2, 3, ...