시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
5 초 | 512 MB | 4 | 2 | 2 | 50.000% |
Rube 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.
7 2 0 2 3 3 0 1 4 5 3 0 1 6 7 1 0 2 1 0 2 1 0 3 1 0 3 5 Q 10 C 2 1 Q 10 C 3 2 Q 10
1 4 1
Here are the paths of the marbles in the sample case: