atcoder#ARC156C. [ARC156C] Tree and LCS
[ARC156C] Tree and LCS
Score : points
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, (10,30) is a subsequence of (10,20,30), but (20,10) is not.
What is a simple path?
For vertices X and Y in a graph G, a walk from X to Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and there is an edge connecting v_i and v_{i+1}. A simple path (or simply a path) is a walk such that v_1,v_2, \ldots, v_k 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.
3
1 2
2 3
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.
4
2 1
2 3
2 4
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
.