k-th ancestor#9629

k-th ancestor (Editorial)

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

\mathit{jump}[x][j] = \mathit{jump}[\mathit{jump}[x][j - 1]][j - 1],

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:

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:

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 >= k

Declare 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);
    }
  }
}