#arc170f. F - Edge Deletion 2

F - Edge Deletion 2

Score: 10001000 points

问题陈述

给定一个有 NN 个顶点的树,顶点编号为 11NN。树的第 ii 条边双向连接顶点 uiu_iviv_i

对于一个排列 P=(P1,,PN)P=(P_1,\ldots,P_N),其中元素为 (1,2,,N)(1,2,\ldots,N) 的排列,我们定义序列 A(P)A(P) 如下:

  • A(P)A(P) 最初为空。在每个顶点 ii 上写上 PiP_i
  • 对于 i=1,2,,Ni=1,2,\ldots,N 按此顺序,执行以下操作:
    • 如果顶点 ii 是孤立顶点,将 00 添加到 A(P)A(P) 的末尾。否则,选择一个写有最小整数的相邻顶点。将选定顶点上的整数添加到 A(P)A(P) 的末尾,并移除连接顶点 ii 和选定顶点的边。

找到所有可能的 A(P)A(P) 中字典序最小的序列。

解决给定的 TT 个测试用例。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a tree with NN vertices numbered 11 to NN. The ii-th edge of the tree connects vertices uiu_i and viv_i bidirectionally.

For a permutation P=(P1,,PN)P=(P_1,\ldots,P_N) of (1,2,,N)(1,2,\ldots,N), we define the sequence A(P)A(P) as follows:

  • A(P)A(P) is initially empty. Write PiP_i on each vertex ii.
  • For i=1,2,,Ni=1,2,\ldots,N in this order, perform the following:
    • If vertex ii is an isolated vertex, append 00 to the end of A(P)A(P). Otherwise, select the adjacent vertex with the smallest written integer. Append the integer written on the selected vertex to the end of A(P)A(P) and remove the edge connecting vertex ii and the selected vertex.

Find the lexicographically smallest sequence among all possible A(P)A(P).

Solve each of the TT given test cases.

Constraints

  • 1T1051 \leq T \leq 10^5
  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1ui,viN1\leq u_i,v_i\leq N
  • The given graph is a tree.
  • All input numbers are integers.
  • The sum of NN over all test cases in a single input is at most 2×1052 \times 10^5.

Input

The input is given from Standard Input in the following format:

TT

case1\mathrm{case}_1

\vdots

caseT\mathrm{case}_T

Each case is given in the following format:

NN

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uN1u_{N-1} vN1v_{N-1}

Output

Print TT lines. The ii-th line (1iT)(1 \leq i \leq T) should contain the answer for the ii-th test case.

Sample Input 1

3
5
1 2
2 3
2 4
4 5
8
8 6
7 2
2 1
3 7
5 6
1 6
4 3
7
7 1
5 2
1 2
6 5
4 1
5 3

Sample Output 1

1 2 0 1 3
1 2 2 3 1 4 0 0
1 2 2 0 3 0 4

In the first test case, for P=(4,1,2,3,5)P=(4,1,2,3,5), one can obtain A(P)=(1,2,0,1,3)A(P)=(1,2,0,1,3) as follows:

  • The vertex adjacent to vertex 11 with the smallest written integer is vertex 22. Append P2=1P_2=1 to the end of A(P)A(P) and remove the edge connecting vertices 11 and 22.

  • The vertex adjacent to vertex 22 with the smallest written integer is vertex 33. Append P3=2P_3=2 to the end of A(P)A(P) and remove the edge connecting vertices 22 and 33.

  • Vertex 33 is an isolated vertex, so append 00 to the end of A(P)A(P).

  • The vertex adjacent to vertex 44 with the smallest written integer is vertex 22. Append P2=1P_2=1 to the end of A(P)A(P) and remove the edge connecting vertices 44 and 22.

  • The vertex adjacent to vertex 55 with the smallest written integer is vertex 44. Append P4=3P_4=3 to the end of A(P)A(P) and remove the edge connecting vertices 55 and 44.

It can be proved that this is the lexicographically smallest sequence among all possible A(P)A(P).