#P9999. [Ynoi2000] tmostnrq2
[Ynoi2000] tmostnrq2
Problem Description
Given a tree with vertices, labeled , and a sequence of length . There are queries. Each query gives . Starting from vertex , move one step toward in order, and output the vertex you finally reach.
If , then moving one step from vertex toward still stays at . Otherwise, you move to the vertex that is adjacent to on the tree and is closest to .
Input Format
The first line contains three integers .
The next line contains integers, representing in order, where is the parent of vertex , and is the root.
The next line contains integers, representing in order.
The next lines each contain three integers , describing one query.
Output Format
Output lines. Each line is the answer to the corresponding query.
5 4 3
1 1 3 3
5 2 2 3
3 4 5
1 3 4
1 2 1
3
2
1
Hint
Idea: Ynoi, Solution: zhoukangyang & ccz181078, Code: zhoukangyang, Data: ccz181078.
Constraints: For of the testdata, .
Translated by ChatGPT 5