luogu#P7830. [CCO 2021] Through Another Maze Darkly

    ID: 7156 远端评测题 8000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2021CCO(加拿大)深度优先搜索 DFS可持久化线段树

[CCO 2021] Through Another Maze Darkly

Background

Warning: abusing the judging system for this problem will result in an account ban!

Problem Description

The Dark Maze is a tree structure with nn rooms and n1n - 1 corridors. The rooms are numbered 1,2,,n1, 2, \cdots, n.

The Dark Maze is completely dark, so you cannot see where you are. To tell directions, each room has a laser pointer, which initially points to one of the corridors connected to that room. You repeatedly follow the strategy below:

  • Rotate the laser pointer in the current room clockwise to the next corridor.
  • Walk along the corridor pointed to by the laser pointer to the other room.

You plan to start from room 11 and repeat this strategy kk times, and you want to know which room you will end up in. You think this problem is too easy, so you make qq queries. Each query is independent, meaning that for each query all laser pointers return to their initial states.

Input Format

The first line contains two integers n,qn, q.

The next nn lines describe the rooms. Line ii describes room ii. First, an integer kk indicates the number of corridors connected to room ii. Then kk integers c1,c2,,ckc_1, c_2, \cdots, c_k are given, indicating, in clockwise order, the room number at the other end of each corridor. The laser pointer in room ii initially points to the corridor leading to room c1c_1.

The next qq lines each contain one positive integer kk.

Output Format

For each query, output one line containing the required value.

5 6
1 2
3 3 1 4
1 2
2 5 2
1 4
1
2
3
4
5
6
2
1
2
4
2
3

Hint

Sample #1 Explanation

The initial directions of the laser pointers are shown in the figure:

Constraints

For 745\frac{7}{45} of the testdata, room ii is connected to rooms i1i - 1 and i+1i + 1 (if those rooms exist).

For another 1445\frac{14}{45} of the testdata, 2n2×1032 \leq n \leq 2 \times 10^3, 1q2×1031 \leq q \leq 2 \times 10^3.

For another 415\frac{4}{15} of the testdata, q=1q = 1.

For 100%100\% of the testdata, 2n8×1052 \leq n \leq 8 \times 10^5, 1q8×1051 \leq q \leq 8 \times 10^5, 1k10151 \leq k \leq 10^{15}. It is guaranteed that the given data forms a tree.

Source

CCO2021 D1T3

Translated by ChatGPT 5