To find the -th ancestor, we use the binary lifting method. For each vertex we store \mathit{jump}[x][j] — its ancestor 2^j levels higher. If there is no such ancestor, the value is . Vertex plays the role of an "empty" vertex: \mathit{jump}[0][j] = 0 for all , so any jump above the root gives .
The table is computed by the formula
that is, a jump of 2^j levels consists of two jumps of 2^{j-1} levels. The value does not exceed 10^5 < 2^{17}, so columns are enough.
Building the table. The pairs in the input come in arbitrary order, and a parent may be described after its child. Therefore, we first fill column for all vertices, then column for all vertices, and so on. By the time column is computed, column j - 1 is already ready for all vertices.
Queries. The tree structure changes only by adding and removing leaves, so the table is easy to maintain:
when a leaf with parent is added, the row of vertex and the rows of all its ancestors are already filled, so the row of is computed entirely at once in O(\log k);
when a leaf is removed, we zero out its row: a leaf has no descendants, so no other vertex refers to it, and the rest of the table does not need to change;
to find the -th ancestor of vertex , we decompose into powers of two and for each set bit perform the jump x = \mathit{jump}[x][j]. If is obtained in the process, there is no -th ancestor. If vertex is not in the tree, its row is zero, and the answer is also .
Complexity. Building the table takes O(p \log k), and each query takes O(\log k). The total complexity of one test is O((p + q) \log k), where \log k \le 17.
Example
Consider the first test. The initial tree has root . The queries 0 5 15, 1 3, 0 15 20, and 0 20 13 add the leaves , , and remove the leaf .
Answers to the queries:
2 15 2: before the other leaves are added, 15 \to 5 \to 2, the answer is ;
2 13 4: k = 4 = 100_2, one jump \mathit{jump}[13][2] = 2, the answer is ;
2 13 3: k = 3 = 11_2, jumps \mathit{jump}[13][0] = 20 and \mathit{jump}[20][1] = 5, the answer is ;
2 6 10: vertex is at depth , there is no tenth ancestor, the answer is ;
2 11 1: vertex is not in the tree, the answer is ;
2 9 1: the parent of vertex is vertex , the answer is .
In the second test, the tree consists of a single vertex . Leaf is added and immediately removed, so in the query 2 4 1 vertex no longer exists, and the answer is .
Algorithm Implementation
Declare the constants. Vertex numbers do not exceed 10^5, so the arrays have size \mathit{MAXN} = 100001. The value in queries does not exceed 10^5 < 2^{17}, so jumps of length 2^0, 2^1, \ldots, 2^{16} are enough, that is, \mathit{LOG} = 17 columns of the table.
#define MAXN 100001
#define LOG 17 // 2^17 > 10^5 >= kDeclare the global variables. \mathit{jump}[x][j] stores the ancestor of vertex located 2^j levels higher, or if there is no such ancestor. The array stores the numbers of the vertices of the initial tree.
int jump[MAXN][LOG]; // jump[x][j] — ancestor of x at distance 2^j (0 — none)
int a[MAXN];The main part of the program. Read the number of tests . Before each test, clear the table so that the data of the previous test does not affect the current one.
scanf("%d", &t);
while (t--)
{
memset(jump, 0, sizeof(jump));
scanf("%d", &p);Read pairs : is the parent of vertex . The vertex number is stored in , and the parent in column zero of the table. For the root y = 0.
for (int i = 0; i < p; i++)
{
scanf("%d %d", &x, &y);
a[i] = x;
jump[x][0] = y;
}Fill the remaining columns of the table. The pairs in the input come in arbitrary order: the parent of a vertex may be described after the vertex itself. Therefore, the table is built by columns: column is computed for all vertices only when column j - 1 is ready for all vertices. The ancestor 2^j levels higher is the ancestor 2^{j-1} levels higher of the ancestor 2^{j-1} levels higher. Since \mathit{jump}[0][j] = 0, a jump above the root always gives .
// by levels: column j is built when column j-1 is ready for all vertices
for (j = 1; j < LOG; j++)
for (i = 0; i < p; i++)
jump[a[i]][j] = jump[jump[a[i]][j - 1]][j - 1];Read the number of queries and process them one by one.
scanf("%d", &q);
while (q--)
{
scanf("%d", &type);Query of type : add a leaf with parent . Vertex is already in the tree, and its row of the table is filled, as are the rows of all its ancestors. Therefore, the row of the new vertex can be computed entirely at once in .
if (type == 0) // add leaf x with parent y
{
scanf("%d %d", &y, &x);
jump[x][0] = y;
for (j = 1; j < LOG; j++)
jump[x][j] = jump[jump[x][j - 1]][j - 1];
}Query of type : remove leaf . Zero out its row of the table — after that, vertex is considered absent. The rows of other vertices do not need to change: a leaf has no descendants, so no other vertex refers to .
else if (type == 1) // remove leaf x
{
scanf("%d", &x);
memset(jump[x], 0, sizeof(jump[x]));
}Query of type : find the -th ancestor of vertex . Write in binary and for each set bit make a jump of length 2^j. If at some step becomes , there is no -th ancestor, and the loop stops. If vertex is not in the tree, its row of the table is zero, and the very first jump gives . Print the resulting value of .
else // k-th ancestor of x
{
scanf("%d %d", &x, &k);
for (j = 0; k && x; j++, k >>= 1)
if (k & 1) x = jump[x][j];
printf("%d\n", x);
}
}
}