#arc170f. F - Edge Deletion 2
F - Edge Deletion 2
Score: points
问题陈述
给定一个有 个顶点的树,顶点编号为 到 。树的第 条边双向连接顶点 和 。
对于一个排列 ,其中元素为 的排列,我们定义序列 如下:
- 最初为空。在每个顶点 上写上 。
- 对于 按此顺序,执行以下操作:
- 如果顶点 是孤立顶点,将 添加到 的末尾。否则,选择一个写有最小整数的相邻顶点。将选定顶点上的整数添加到 的末尾,并移除连接顶点 和选定顶点的边。
找到所有可能的 中字典序最小的序列。
解决给定的 个测试用例。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given a tree with vertices numbered to . The -th edge of the tree connects vertices and bidirectionally.
For a permutation of , we define the sequence as follows:
- is initially empty. Write on each vertex .
- For in this order, perform the following:
- If vertex is an isolated vertex, append to the end of . Otherwise, select the adjacent vertex with the smallest written integer. Append the integer written on the selected vertex to the end of and remove the edge connecting vertex and the selected vertex.
Find the lexicographically smallest sequence among all possible .
Solve each of the given test cases.
Constraints
- The given graph is a tree.
- All input numbers are integers.
- The sum of over all test cases in a single input is at most .
Input
The input is given from Standard Input in the following format:
Each case is given in the following format:
Output
Print lines. The -th line should contain the answer for the -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 , one can obtain as follows:
-
The vertex adjacent to vertex with the smallest written integer is vertex . Append to the end of and remove the edge connecting vertices and .
-
The vertex adjacent to vertex with the smallest written integer is vertex . Append to the end of and remove the edge connecting vertices and .
-
Vertex is an isolated vertex, so append to the end of .
-
The vertex adjacent to vertex with the smallest written integer is vertex . Append to the end of and remove the edge connecting vertices and .
-
The vertex adjacent to vertex with the smallest written integer is vertex . Append to the end of and remove the edge connecting vertices and .
It can be proved that this is the lexicographically smallest sequence among all possible .