#P13047. [GCJ 2021 Finals] Infinitree

    ID: 12848 Type: RemoteJudge 90000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 7 Uploaded By: Tags>2021矩阵加速最近公共祖先 LCAGoogle Code Jam

[GCJ 2021 Finals] Infinitree

题目描述

This problem is about finding the distance between two nodes of a strictly binary tree. Oh, is that too easy?! Ok, the tree is potentially infinite now. Keep it up and we will start going up the aleph numbers.

In this problem, a tree is either a single node XX, or a node XX with two trees attached to it: a left subtree and a right subtree. In both cases, XX is the root of the tree. If the tree is not a single node, the roots of both the left and right subtrees are the only children of XX.

There is a set of colors numbered from 0 to N\mathbf{N}, inclusive. Each node is of exactly one color. There might be zero, one, or multiple nodes of each color. Each node of color 0 (white) is a leaf node (that is, it has no children). Each node of color ii, for 1iN1 \leq i \leq \mathbf{N}, has exactly 2 children: the left one is color Li\mathbf{L}_{i} and the right one is color Ri\mathbf{R}_{i}. The root of the tree is color 1 (black). Note that the tree may have a finite or countably infinite number of nodes.

For example, the following picture illustrates a finite tree defined by the lists L=[3,0,0]\mathbf{L}=[3,0,0] and R=[2,0,2]\mathbf{R}=[2,0,2]. Color 2 is blue and color 3 is yellow.

The distance between two nodes in the tree is the minimum number of steps that are needed to get from one node to the other. A step is a move from a node to its direct parent or its direct child.

Nodes in the tree are indexed using positive integers. The root has index 11. Then, other nodes are indexed using consecutive integers, with nodes with smaller distances to the root being indexed first. For nodes that are equidistant to the root, nodes that are further to the left are indexed first. For example, the following picture adds indices to each node in the tree we presented before. Notice that each node's index is independent from its color.

As another example, the following picture shows the first 3333 nodes of an infinite tree defined by the lists L=[3,4,2,4]\mathbf{L}=[3,4,2,4] and R=[2,2,4,0]\mathbf{R}=[2,2,4,0]. Color 44 is green.

Given the lists L\mathbf{L} and R\mathbf{R} that define a tree and the indices of two different nodes in the tree, return the distance between those two nodes.

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case consists of three lines. The first line contains N\mathbf{N}, A\mathbf{A}, and B\mathbf{B}: the size of the lists that define the tree, and the indices of the two nodes whose distance you need to calculate, respectively. The second line contains N\mathbf{N} integers L1\mathbf{L}_{1}, L2\mathbf{L}_{2}, \ldots, LN\mathbf{L}_{\mathbf{N}} and the third line contains N\mathbf{N} integers R1\mathbf{R}_{1}, R2\mathbf{R}_{2}, \ldots, RN\mathbf{R}_{\mathbf{N}}, as described above.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the distance between the nodes with indices A\mathbf{A} and B\mathbf{B} in the tree defined by the lists L\mathbf{L} and R\mathbf{R}.

5
3 1 8
3 0 0
2 0 2
3 1 5
3 0 0
2 0 2
4 1 27
3 4 2 4
2 2 4 0
4 1 28
3 4 2 4
2 2 4 0
3 1 10
1 3 1
3 2 1
Case #1: 3
Case #2: 2
Case #3: 4
Case #4: 5
Case #5: 3
4
3 5 7
3 0 0
2 0 2
3 4 9
3 0 0
2 0 2
4 11 18
3 4 2 4
2 2 4 0
4 21 22
3 4 2 4
2 2 4 0
Case #1: 4
Case #2: 3
Case #3: 5
Case #4: 8

提示

Sample Explanation

The tree in Sample Cases #1 and #2 is the first tree shown in the statement. The tree in Sample Cases #3 and #4 is the last tree shown in the statement. The same is true for the additional samples below. In Sample Case #5, notice that some colors may not be present in the tree.

Sample Test Set 2 fits the limits of Test Set 2. It will not be run against your submitted solutions.

Limits

  • 1T1001 \leq \mathbf{T} \leq 100.
  • 1N501 \leq \mathbf{N} \leq 50.
  • 0LiN0 \leq \mathbf{L}_{i} \leq \mathbf{N}.
  • 0RiN0 \leq \mathbf{R}_{i} \leq \mathbf{N}.
  • A<B1018\mathbf{A} < \mathbf{B} \leq 10^{18}.
  • The tree defined by L\mathbf{L} and R\mathbf{R} has at least B\mathbf{B} nodes.

Test Set 1 (25 Pts, Visible Verdict)

  • A=1\mathbf{A} = 1.

Test Set 2 (40 Pts, Hidden Verdict)

  • 1A10181 \leq \mathbf{A} \leq 10^{18}.