각 노드가 이웃을 순환하며 구슬을 보내는 트리에서, 일부 노드의 활성 간선을 바꾸는 갱신과 x걸음 뒤 구슬의 위치를 묻는 질의를 처리한다.
어려움9트리이분 탐색누적 합아직 제출이 없습니다시간 제한5초메모리 제한512 MBRube has constructed a brand new obscure contraption and is now testing it. The machine has n nodes numbered from 1 to n that can accomodate a marble, and n−1 tracks connecting the nodes. It is possible to get from any node to any other by following the tracks, that is, the tracks form an undirected tree.
For every node, the tracks adjacent to it are ordered: if a node v has kv tracks adjacent to it, we will write ev,0, . . . , ev,kv−1 for the order in question. Further, every node has an active adjacent track selected: we will write tv ∈ {0, . . . , kv − 1} to denote that the track etv is active for the node v.
Here’s how the machine operates. First, a marble is placed at the node 1. Then, one or more steps take place. Each step proceeds as follows:
Rube wants you to process several queries of two kinds:
Rube is very busy tinkering with his device, so he wants to you answer fast!
The first line contains a single integer n (2 ≤ n ≤ 105): the number of nodes in the machine.
The following n lines describe the tracks. The i-th of these lines contains two integers ki and ti (0 ≤ ti ≤ ki − 1), followed by ki distinct integers ui,0, . . . , uv,kv−1 (ui,j ≠ i). This denotes:
It is guaranteed that the described tracks form an undirected tree, that is:
The following line contains a single integer q (1 ≤ q ≤ 105): the number of queries.
The following q lines describe the queries in the format described above, one per line.
Print answers for all “Q x” queries in the order asked, one per line.
Here are the paths of the marbles in the sample case: