#arc156c. [arc156_c]Tree and LCS
[arc156_c]Tree and LCS
Problem Statement
We have a tree with vertices numbered to . The -th edge of connects vertex and vertex .
Let us use to define the similarity of a permutation of as follows.
- For a simple path in , let . The similarity is the maximum possible length of a longest common subsequence of and .
Construct a permutation with the minimum similarity.
What is a subsequence? A subsequence of a sequence is a sequence obtained by removing zero or more elements from that sequence and concatenating the remaining elements without changing the relative order. For instance, is a subsequence of , but is not. What is a simple path? For vertices and in a graph , a walk from to is a sequence of vertices such that , , and there is an edge connecting and . A simple path (or simply a path) is a walk such that are all different.
Constraints
- The given graph is a tree.
- All numbers in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print a permutation with the minimum similarity, separated by spaces. If multiple solutions exist, you may print any of them.
Sample Input 1
3
1 2
2 3
Sample Output 1
3 2 1
This permutation has a similarity of , which can be computed as follows.
-
For , we have . The length of a longest common subsequence of and is .
-
For , we have . The length of a longest common subsequence of and is .
-
For , we have . The length of a longest common subsequence of and is .
-
For , we have . The length of a longest common subsequence of and is . The same goes for , the reversal of .
-
For , we have . The length of a longest common subsequence of and is . The same goes for , the reversal of .
-
For , we have . The length of a longest common subsequence of and is . The same goes for , the reversal of .
We can prove that no permutation has a similarity of or less, so this permutation is a valid answer.
Sample Input 2
4
2 1
2 3
2 4
Sample Output 2
3 4 1 2
If multiple permutations have the minimum similarity, you may print any of them. For instance, you may also print 4 3 2 1
.