#P14194. [ICPC 2024 Hangzhou R] Heavy-light Decomposition

    ID: 14067 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>2024Special JudgeICPC杭州

[ICPC 2024 Hangzhou R] Heavy-light Decomposition

题目描述

Heavy-light Decomposition\textit{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 nn vertices, numbered from 11 to nn. 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 vv, let uu be its parent. Vertex vv can be classified as heavy only if the size of its subtree is among the largest of all the children of uu. More formally, denote the size of the subtree rooted at vertex vv as svs_v, and let ch(u)\text{ch}(u) represent the set of children of uu. Then, vv can be heavy only if svsws_v \geq s_w for all wch(u)w \in \text{ch}(u). Note that there might be several children of uu 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 x1,x2,,xkx_1, x_2, \ldots, x_k satisfying all the following constraints.

  • x1x_1 is either the root or a light vertex.
  • For all 2ik2 \leq i \leq k, xix_i is xi1x_{i-1}'s child and is a heavy vertex.
  • xkx_k 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 1212 vertices, where the heavy chains are [1,2,3,4,5][1, 2, 3, 4, 5], [9,10,11][9, 10, 11], [7,8][7, 8], [6][6], and [12][12].

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 nn of the original tree and kk heavy chains. The ii-th chain is described by two integers lil_i and rir_i, indicating a heavy chain li,li+1,,ril_i, l_i + 1, \ldots, r_i.

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 nn vertices numbered from 11 to nn.
  • The kk 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 TT (1T1051 \le T \le 10^5) indicating the number of test cases. For each test case:

The first line contains two integers nn and kk (1n1051 \leq n \leq 10^5, 1kn1 \leq k \leq n), indicating the number of vertices of the tree and the number of heavy chains after its HLD.

For the following kk lines, the ii-th line contains two integers lil_i and rir_i (1lirin1 \leq l_i \leq r_i \leq n), indicating that the ii-th heavy chain is li,li+1,,ril_i, l_i + 1, \ldots, r_i.

It is guaranteed that each of the nn vertices belongs to exactly one given heavy chain. It is also guaranteed that the sum of nn of all test cases does not exceed 2×1052 \times 10^5.

输出格式

For each test case:

If it is possible to construct such a tree, output one line containing nn integers p1,p2,,pnp_1, p_2, \cdots, p_n separated by a space, where pip_i is the parent of vertex ii. pi=0p_i = 0 indicates that vertex ii 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 IMPOSSIBLE\texttt{IMPOSSIBLE} 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.