Amazing Tree

시간 제한1초메모리 제한1024 MB

요약
트리에서 시작 정점과 각 정점의 이웃 순서를 정해 DFS 후위 순회 목록이 사전순으로 가장 작게 만든다.
난이도

보통10점 중 7점

유형
DFS, 그리디, 트리, 정렬
정답자
아직 제출이 없습니다

문제

Consider an undirected tree. The following algorithm constructs a post-order traversal of the tree:

fun dfs(v):
    mark v as used
    for u in neighbours(v):
        if u is not used:
            dfs(u)
    append v to order

The post-order traversal will be in the list order.

You are allowed to choose the order of neighbors for each vertex as well as the starting vertex. What is the lexicographically minimal order you can get?

입력

The first line of input contains one integer TT (1≤T≤1051 \le T \le 10^{5}) --- the number of test cases you need to process. Description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^{5}) --- the number of vertices in the tree.

The ii-th of the next n−1n-1 lines contains two integers u_i,v_iu\_i, v\_i (1≤u_i,v_i≤n1 \le u\_i, v\_i \le n, u_i≠v_iu\_i \ne v\_i), meaning that there is an undirected edge (u_i,v_i)(u\_i, v\_i) in the tree. It is guaranteed that the given graph is a tree.

The sum of nn over all test cases in one test file does not exceed 2⋅1052 \cdot 10^{5}.

출력

For each test case print the lexicographically minimal order on a separate line.

힌트

The first test looks as follows:

By starting in vertex 11 we can only get order 2 3 12\ 3\ 1. By starting in vertex 22 we can only get order 1 3 21\ 3\ 2. By starting in vertex 33 we can get two orders: 1 2 31\ 2\ 3 and 2 1 32\ 1\ 3. The lexicographically minimal of the four orders is 1 2 31\ 2\ 3.

The second test looks as follows:

By starting in vertex 11 we can get two orders: 2 3 12\ 3\ 1 and 3 2 13\ 2\ 1. By starting in vertex 22 we can only get order 3 1 23\ 1\ 2. By starting in vertex 33 we can only get order 2 1 32\ 1\ 3. The lexicographically minimal of the four orders is 2 1 32\ 1\ 3.

The third test looks as follows:

The lexicographically minimal order is 4 5 2 1 6 3 74\ 5\ 2\ 1\ 6\ 3\ 7 it can be obtained by starting in node 77.

예제1

  1. 예제 1

    입력
    3
    3
    1 3
    3 2
    3
    2 1
    1 3
    7
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    
    예상 출력
    1 2 3
    2 1 3
    4 5 2 1 6 3 7