QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100 Difficulty: [show] Hackable ✓

#2009. Numbers on the Tree

Statistics

Given a tree of size $n$, it has $n$ nodes and $n-1$ edges, with nodes numbered $1 \sim n$. Initially, each node contains a number from $1 \sim n$, and each number from $1 \sim n$ appears on exactly one node.

You need to perform exactly $n-1$ edge deletion operations. In each operation, you must choose an edge that has not yet been deleted. The numbers on the two nodes connected by this edge will be swapped, and then the edge will be deleted.

After $n-1$ operations, all edges will have been deleted. At this point, by arranging the node indices where the numbers $1 \sim n$ are located in increasing order of the numbers, we obtain a sequence of node indices $P_i$. Please find the lexicographically smallest $P_i$ that can be obtained under an optimal operation strategy.

Input

The input contains multiple test cases. The first line contains a positive integer $T$, representing the number of test cases. For each test case: The first line contains an integer $n$, representing the size of the tree. The second line contains $n$ integers, where the $i$-th ($1 \le i \le n$) integer represents the initial node index of the number $i$. The next $n-1$ lines each contain two integers $x, y$, representing an edge connecting node $x$ and node $y$.

Output

For each test case, output a single line containing $n$ space-separated integers, representing the lexicographically smallest $P_i$ obtainable under an optimal operation strategy.

Examples

Input 1

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

Output 1

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

Examples 2

See tree/tree2.in and tree/tree2.ans in the contestant directory.

Constraints

Test Case ID $n \le$ Special Property
$1 \sim 2$ $10$ None
$3 \sim 4$ $160$ The tree is a chain
$5 \sim 7$ $2000$ The tree is a chain
$8 \sim 9$ $160$ There exists a node with degree $n-1$
$10 \sim 12$ $2000$ There exists a node with degree $n-1$
$13 \sim 16$ $160$ None
$17 \sim 20$ $2000$ None

For all test cases: $1 \le T \le 10$, and the input is guaranteed to be a tree.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.