#P70. [CSP-S 2019 Day 1] Cut and Swap
[CSP-S 2019 Day 1] Cut and Swap
Person in Charge
Problem Description
Given a tree of size , it has nodes and edges, with nodes numbered from to . Initially, each node has a number from to , and each number from to appears on exactly one node.
Next, you need to perform exactly edge deletion operations. In each operation, you select an edge that has not been deleted yet. At this time, the numbers on the two nodes connected by this edge will be swapped, and then the edge will be deleted.
After operations, all edges will be deleted. At this point, arrange the node numbers where the numbers are located in ascending order of the numbers to obtain a permutation of node numbers. Now, please find the lexicographically smallest that can be obtained under the optimal operation scheme.
As shown in the left figure, the numbers in the blue circles are initially on nodes ②, ①, ③, ⑤, ④ respectively. Deleting all edges in the order of (1)(4)(3)(2), the tree becomes the right figure. The permutation of node numbers obtained in the order of the numbers is ①③④②⑤, which is the lexicographically smallest among all possible results.
(The following images are from LibreOJ)

Input Format
This input contains multiple test cases.
The first line contains a positive integer , representing the number of test cases.
For each test case:
- The first line contains an integer , representing the size of the tree.
- The second line contains integers, where the -th integer () represents the node number where the number is initially located.
- The next lines each contain two integers and , representing an edge connecting node and node .
Output Format
For each test case, output a line with integers separated by spaces, representing the lexicographically smallest obtainable under the optimal operation scheme.
Sample 1
See tree/tree1.in and tree/tree1.ans in the contestant's directory.
Sample 2
See tree/tree2.in and tree/tree2.ans in the contestant's directory.
Data Range
| Test Case ID | Special Property | |
|---|---|---|
| None | ||
| The tree is a chain | ||
| There exists a node with degree | ||
| None | ||
For of the data: , , the given graph is a tree, and the initial numbers form a permutation of .