atcoder#ARC156C. [ARC156C] Tree and LCS

[ARC156C] Tree and LCS

Score : 600600 points

Problem Statement

We have a tree TT with vertices numbered 11 to NN. The ii-th edge of TT connects vertex uiu_i and vertex viv_i.

Let us use TT to define the similarity of a permutation P=(P1,P2,,PN)P = (P_1,P_2,\ldots,P_N) of (1,2,,N)(1,2,\ldots,N) as follows.

  • For a simple path x=(x1,x2,,xk)x=(x_1,x_2,\ldots,x_k) in TT, let y=(Px1,Px2,,Pxk)y=(P_{x_1}, P_{x_2},\ldots,P_{x_k}). The similarity is the maximum possible length of a longest common subsequence of xx and yy.

Construct a permutation PP 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

  • 2N50002 \leq N \leq 5000
  • 1ui,viN1\leq u_i,v_i\leq N
  • 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:

NN

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uN1u_{N-1} vN1v_{N-1}

Output

Print a permutation PP 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 11, which can be computed as follows.

  • For x=(1)x=(1), we have y=(P1)=(3)y=(P_1)=(3). The length of a longest common subsequence of xx and yy is 00.
  • For x=(2)x=(2), we have y=(P2)=(2)y=(P_2)=(2). The length of a longest common subsequence of xx and yy is 11.
  • For x=(3)x=(3), we have y=(P2)=(1)y=(P_2)=(1). The length of a longest common subsequence of xx and yy is 00.
  • For x=(1,2)x=(1,2), we have y=(P1,P2)=(3,2)y=(P_1,P_2)=(3,2). The length of a longest common subsequence of xx and yy is 11. The same goes for x=(2,1)x=(2,1), the reversal of (1,2)(1,2).
  • For x=(2,3)x=(2,3), we have y=(P2,P3)=(2,1)y=(P_2,P_3)=(2,1). The length of a longest common subsequence of xx and yy is 11. The same goes for x=(3,2)x=(3,2), the reversal of (2,3)(2,3).
  • For x=(1,2,3)x=(1,2,3), we have y=(P1,P2,P3)=(3,2,1)y=(P_1,P_2,P_3)=(3,2,1). The length of a longest common subsequence of xx and yy is 11. The same goes for x=(3,2,1)x=(3,2,1), the reversal of (1,2,3)(1,2,3).

We can prove that no permutation has a similarity of 00 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.