#P15993. [PA 2026] Prüfer 序列 / Kod Prüfera

[PA 2026] Prüfer 序列 / Kod Prüfera

Background

$\large{\bf{\textcolor{red}{Warning: Abusing the judging system for this problem may get your account banned, even after just once, and may also result in an IP ban.}}}$

$\large{\bf{\textcolor{red}{Several people have already received school disciplinary actions due to related behavior. Learn from others’ mistakes.}}}$

Due to differences in judge machine performance, the time limit is reduced by 55 seconds.

Problem Description

Given a tree with nn vertices (where n>2n > 2), with vertices numbered from 11 to nn, its Prüfer sequence is a uniquely determined sequence of length n−2n - 2, which can be obtained by the following simple algorithm:

While the tree has more than two vertices:

  • Find the vertex with degree 11 that has the smallest label.
  • Append the label of its only neighbor to the sequence.
  • Delete this vertex from the tree.

It can be proven that every sequence of length n−2n - 2 consisting of numbers between 11 and nn is the Prüfer sequence of some tree, and the Prüfer sequence uniquely determines the tree it comes from. For these and other interesting facts about Prüfer sequences, you may refer to OI-wiki or other materials.

In this problem, we are given a tree and consider the Prüfer sequences generated by different ways of labeling its vertices. If SS is a labeling of vertices (formally, an injective function from the vertex set to the set {1,…,n}\{1,\dots,n\}), then let K(S)K(S) denote the Prüfer sequence of the tree under this labeling.

Your task is to find the lexicographically smallest Prüfer sequence of the given tree, i.e. find a labeling SS such that the sequence equals K(S)K(S), and for any other labeling S′S', either K(S)=K(S′)K(S) = K(S'), or at the first position where K(S)K(S) and K(S′)K(S') differ, the value in K(S′)K(S') is larger.

You need to solve this problem for tt independent test cases.

Input Format

The first line contains an integer tt (1≤t≤10001 \leq t \leq 1000), the number of test cases.

Each test case starts with a line containing an integer nn (3≤n≤10003 \leq n \leq 1000), the number of vertices in the tree. The vertices are numbered from 11 to nn, but this numbering does not necessarily correspond to the lexicographically smallest Prüfer sequence.

The next n−1n - 1 lines describe the edges of the tree. Each line contains two integers aia_i and bib_i (1≤ai,bi≤n, ai≠bi1 \leq a_i, b_i \leq n,\ a_i \neq b_i), indicating an edge between vertex aia_i and vertex bib_i.

The sum of nn over all test cases does not exceed 5000.

Output Format

Output tt lines, one for each test case. For the ii-th line, output a sequence of length n−2n - 2, which is the lexicographically smallest Prüfer sequence of the tree in the ii-th test case under an optimal vertex labeling.

2
5
1 2
2 3
3 4
3 5
16
8 1
9 1
10 1
11 2
12 2
2 3
13 4
4 3
14 5
15 5
5 3
3 1
1 6
6 7
7 16
1 1 2
1 1 1 2 2 3 4 3 5 5 3 1 6 7

Hint

Sample Explanation

In the first test case, an example of a vertex labeling that yields the lexicographically smallest Prüfer sequence is 1→51 \to 5, 2→22 \to 2, 3→13 \to 1, 4→44 \to 4, 5→35 \to 3.
With this labeling, in the first step of the Prüfer sequence algorithm, it will choose the vertex whose new label is 33 (and append 11 to the sequence, i.e. the new label of its only neighbor). In the second step, it will choose the vertex whose new label is 44 (its only neighbor is also 11). In the third step (after deleting vertices 33 and 44), vertex 11 has become a leaf and will be chosen, and the label of its neighbor, 22, will be appended as the last element of the sequence.
In the second test case, the optimal solution is to not relabel the vertices at all. Also, the order of edges in the input matches the order in which the Prüfer sequence algorithm deletes leaf vertices.

Translated by ChatGPT 5