#P5903. 【模板】树上 K 级祖先
【模板】树上 K 级祖先
Description
Given a rooted tree with nodes.
There are queries. In the -th query, and are given. You need to find the -th ancestor of node , and denote the answer as . In particular, .
The queries in this problem are generated within the program.
A random seed and a random function are given:
#define ui unsigned int
ui s;
inline ui get(ui x) {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return s = x;
}
You need to generate the queries in order.
Let be the depth of node , where the root has depth .
For the -th query, $x_i = ((\operatorname{get}(s) \operatorname{xor} ans_{i-1}) \bmod n) + 1$, and $k_i = (\operatorname{get}(s) \operatorname{xor} ans_{i-1}) \bmod d_{x_i}$.
Input Format
The first line contains three integers .
The second line contains integers , where is the parent of . In particular, if , then is the root.
Output Format
Output one integer per line, representing .
6 3 7
5 5 2 2 0 3
1
Hint
[Sample Explanation]
, , .
, , .
, , .
Therefore, the output is .
For of the testdata, .
For of the testdata, .
For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号