#P3248. [HNOI2016] 树
[HNOI2016] 树
Description
Little A wants to build a very large tree, but his materials are limited, so he uses a small trick. Initially, Little A has a tree with nodes, numbered , where node is the root; we call this the "template tree". Little A decides to construct a large tree based on this template tree. The construction proceeds as follows:
-
Copy the template tree as the initial large tree.
-
Repeat steps (2.1)–(2.3) times.
(2.1) Choose two integers , where , the current number of nodes in the large tree.
(2.2) Copy the subtree of the template tree rooted at node , and attach it under node in the large tree (that is, after copying, the subtree rooted at node from the template tree becomes a subtree of node in the large tree).
(2.3) Renumber the newly added nodes in the large tree according to their order in the template tree. For example, suppose the large tree has nodes before step (2.2), and the subtree rooted at in the template tree has nodes. Then the newly added nodes will be numbered in the large tree; the relative order of these node numbers in the large tree is consistent with the order of the corresponding nodes in the template tree. An example is given below. Suppose the template tree is as follows: 
According to step (1), the initial large tree is the same as the template tree.
In step (2.1), suppose we choose . After performing steps (2.2) and (2.3), the new large tree is as shown below:

Now he asks you for the distances between some pairs of nodes in the tree.
Input Format
The first line contains three integers , separated by spaces, where is the number of nodes in the template tree, is the number of repetitions of the operation in step (2), and is the number of queries.
The next lines each contain two integers , representing an edge in the template tree.
Then there are lines, each containing two integers , representing one operation that copies the subtree rooted at in the template tree and attaches it as a subtree of node in the large tree.
Then there are lines, each containing two integers , asking for the distance between nodes and in the large tree.
.
Output Format
Output lines, each containing one integer. The -th line is the answer to the -th query.
5 2 3
1 4
1 3
4 2
4 5
4 3
3 2
6 9
1 8
5 3
6
3
3
Hint
After two operations, the large tree becomes the shape shown below:

There are edges between nodes and , so the distance is . Similarly, there are edges between nodes and . There are also edges between nodes and .
Translated by ChatGPT 5
京公网安备 11011102002149号