#P3995. 树链剖分
树链剖分
Description
Daning has been studying heavy-light decomposition. He found that the time complexity of heavy-light decomposition mainly depends on how heavy and light chains are divided, and the most common method is to split according to subtree size. As shown in the figure (from Baidu Baike), black edges are heavy chains (arbitrary length), and white edges are light chains (all of length . Note that in the figure, – and – are different light chains.

For each node, its child on the heavy chain is called its heavy child, and there is exactly one; leaf nodes have no heavy child. For example, for node in the figure, its heavy child is node . Clearly, different decompositions lead to different numbers of chains visited by the same set of queries. Now, given a rooted tree with root and several queries, each query traverses all heavy-light chains along the path from to once. For example, under the decomposition in the figure above, the query from to visits chains in total: the light chain –, the heavy chain –, and the light chain –; the query from to also visits chains: the light chain –, the light chain –, and the heavy chain –.
Please output a decomposition scheme that minimizes the total number of heavy-light chains visited over all queries. Since there may be many valid schemes, output any one of them. We provide a Special Judge to check the correctness of your scheme.
Let the total number of chains visited by your scheme be , by the standard solution be , and let the scoring parameter be .
Your score is:
- points if .
- points if .
- points if .
- points if .
- point if you output a valid scheme.
, where is the number of queries.
We provide Div_Checker.exe to check your answer. Put it in the same folder as div.in and div.out, then run it, where div.in is the input file and div.out is your program’s output on that input. If your div.out is valid, it will print:
Your answer is XXX.
XXX is the total number of chains visited by your decomposition on that input. Otherwise, it will print:
Wrong Outdata.
Note: You must not use file I/O in the official submission.
Input Format
The first line contains two positive integers and , the number of nodes of the tree and the number of queries.
The next lines each contain two positive integers , , meaning there is an edge between and .
The next lines each describe a query with two integers , , meaning a query from to .
Output Format
Output lines. For node , if it is not a leaf, output its heavy child in your decomposition; otherwise, output .
14 7
1 4
4 10
4 9
4 8
9 13
13 14
3 1
7 3
2 1
2 6
6 12
11 6
5 2
11 3
7 8
2 8
11 1
8 14
5 7
9 14
2
6
7
8
0
11
0
0
13
0
0
0
14
0
Hint
The sample corresponds to the figure above, but the decomposition in the figure is not optimal for the sample queries.
Constraints:
- For of the testdata, .
- For of the testdata, .
- For of the testdata, , . It is guaranteed the input is a valid tree.
If you are not sure how to use the checker, please refer to the images below.
The data in the images is the sample.

An output of a valid scheme.

An invalid output.

: Added a new set of hack testdata.
Translated by ChatGPT 5
京公网安备 11011102002149号