#P14194. [ICPC 2024 Hangzhou R] Heavy-light Decomposition
[ICPC 2024 Hangzhou R] Heavy-light Decomposition
题目描述
(HLD) is a useful technique applied to trees for efficiently querying chains of vertices. Let's first review the definition of HLD in case you forget.
You are given a rooted tree with vertices, numbered from to . You need to classify each non-root vertex as either heavy or light. Each non-leaf vertex has exactly one child classified as heavy, and the remaining vertices as light.
For any non-root vertex , let be its parent. Vertex can be classified as heavy only if the size of its subtree is among the largest of all the children of . More formally, denote the size of the subtree rooted at vertex as , and let represent the set of children of . Then, can be heavy only if for all . Note that there might be several children of satisfying this constraint; in this case, you should choose one of them to be heavy, and the others to be light.
After that, all vertices of the tree can be decomposed into several non-overlapping heavy chains, where each vertex belongs to exactly one heavy chain. A heavy chain is a sequence of vertices satisfying all the following constraints.
- is either the root or a light vertex.
- For all , is 's child and is a heavy vertex.
- is a leaf.
:::align{center}

An HLD example. Heavy chains are marked with solid red edges. :::
For example, the above figure shows a valid HLD of the given tree with vertices, where the heavy chains are , , , , and .
Pig100Ton is a very experienced competitive programming contestant in Pigeland. He wants to know whether it is possible to recover the original tree from the heavy chains after HLD. Specifically, he will give you the number of vertices of the original tree and heavy chains. The -th chain is described by two integers and , indicating a heavy chain .
Your task is to construct a tree satisfying the following constraints or to tell Pig100Ton it is impossible:
- It is a rooted tree that contains vertices numbered from to .
- The chains provided by Pig100Ton should form a valid HLD of the tree. A valid HLD refers to a valid way to classify each non-root vertex as either light or heavy, and then decompose the tree into several non-overlapping heavy chains.
输入格式
There are multiple test cases. The first line of the input contains an integer () indicating the number of test cases. For each test case:
The first line contains two integers and (, ), indicating the number of vertices of the tree and the number of heavy chains after its HLD.
For the following lines, the -th line contains two integers and (), indicating that the -th heavy chain is .
It is guaranteed that each of the vertices belongs to exactly one given heavy chain. It is also guaranteed that the sum of of all test cases does not exceed .
输出格式
For each test case:
If it is possible to construct such a tree, output one line containing integers separated by a space, where is the parent of vertex . indicates that vertex is the root. If there are multiple valid solutions, you may output any one of them.
If it is not possible to construct such a tree, just output in one line.
3
12 5
1 5
9 11
7 8
6 6
12 12
4 3
1 1
4 4
2 3
2 2
1 1
2 2
0 1 2 3 4 3 2 7 1 9 10 10
2 0 2 2
IMPOSSIBLE
提示
For the first test case, the sample output is the tree shown in the description.