We solve the problem using the binary lifting method. Climbing levels up from an employee one step at a time is too slow: with , up to 2 \cdot 10^5 this may take about 4 \cdot 10^{10} operations. Therefore, for every vertex we precompute its ancestors located 1, 2, 4, 8, \ldots levels above it.
Let \mathit{up}[i][j] be the number of the boss of employee located 2^j levels above. If no such boss exists, we set \mathit{up}[i][j] = 0. It is convenient to treat as a dummy vertex "above the root", for which \mathit{up}[0][j] = 0 for all .
Column j = 0 contains the direct bosses: \mathit{up}[i][0] = e_i, and \mathit{up}[1][0] = 0. The remaining columns are computed using the recurrence: to climb 2^j levels, we climb 2^{j-1} levels twice:
How do we find the boss levels above? Write in binary — then is a sum of distinct powers of two, and climbing levels splits into several jumps of length 2^j, each of which is performed in using the table. For example, for k = 13 = 1101_2 = 8 + 4 + 1 we make three jumps:
x = \mathit{up}[x][3] — climb levels;
x = \mathit{up}[x][2] — climb more levels;
x = \mathit{up}[x][0] — climb more level.
After each jump we land at an actual vertex of the tree, and the next jump is made from it. If the required boss does not exist, at some jump the value of becomes and never changes afterwards, since \mathit{up}[0][j] = 0 for all . Therefore, the answer is printed if and only if x = 0 at the end.
The table takes O(n \log n) memory and is built in O(n \log n). Each query is processed in O(\log n), since the number has at most bits. The overall complexity is O((n + q) \log n).
Example
The hierarchy of employees from the example looks as follows:
Let us compute some values of the table:
\mathit{up}[4][0] = \mathit{up}[5][0] = 3 (the direct boss);
\mathit{up}[4][1] = \mathit{up}[5][1] = 1 (the boss of the boss);
\mathit{up}[2][0] = \mathit{up}[3][0] = 1.
The complete binary lifting table for the example looks like this (all other cells are ):
Let us consider the queries.
Query : k = 1 = 1_2. We perform x = \mathit{up}[4][0] = 3. Answer: .
Query : k = 2 = 10_2. We perform x = \mathit{up}[4][1] = 1. Answer: .
Query : k = 3 = 11_2. We perform x = \mathit{up}[4][0] = 3, then x = \mathit{up}[3][1] = 0. We got — vertex is at depth , so it is impossible to climb levels up. Answer: .
Algorithm Implementation
Declare the binary lifting table . The value \mathit{up}[i][j] equals the number of the boss of employee located 2^j levels above, or if no such boss exists. Since k \le n \le 2 \cdot 10^5 < 2^{18}, twenty columns (\mathit{logK} = 20) are certainly enough.
#define maxN 200001
#define logK 20
int up[maxN][logK];Read the input data. For each employee (2 \le i \le n), read the direct boss and store it in . For the general director \mathit{up}[1][0] = 0, since the global array is zero-initialized.
scanf("%d %d", &n, &q);
for (i = 2; i <= n; i++)
scanf("%d", &up[i][0]);Fill the binary lifting table column by column. Climbing 2^j levels means climbing 2^{j-1} levels twice:
By the time column is computed, column j - 1 is already completely filled, so the outer loop runs over . If it is impossible to climb 2^j levels, the cell automatically gets : either \mathit{up}[i][j - 1] = 0, or the intermediate ancestor exists, but its cell in column j - 1 equals .
for (j = 1; j < logK; j++)
for (i = 1; i <= n; i++)
up[i][j] = up[up[i][j - 1]][j - 1];Process the queries one by one. For each query, read the employee and the number of levels .
for (t = 0; t < q; t++)
{
scanf("%d %d", &x, &k);Decompose into powers of two. Iterate over the bits of : if the -th bit is set, climb from the current vertex directly 2^i levels up by performing x = \mathit{up}[x][i]. The order of the bits does not matter — the total climb is still . If at some step becomes , it stays , since \mathit{up}[0][i] = 0.
for (i = 0; i < logK; i++)
if (k & (1 << i))
x = up[x][i];If after all the jumps x = 0, we have climbed above the general director, and the required boss does not exist — print . Otherwise, print .
printf("%d\n", x ? x : -1);
}