#P7920. [Kubic] Permutation

[Kubic] Permutation

Description

For a permutation pp of 1n1 \sim n, define GpG_p as an undirected graph constructed by the following method:

  • For each i(1,n]i \in (1,n], find the largest j[1,i)j \in [1,i) such that pi>pjp_i > p_j, and then add an edge between ii and jj. If no such jj exists, do not add an edge.

You are given a tree TT with nn nodes.

Call pp a good permutation if and only if GpG_p is isomorphic to TT.

If a good permutation exists, output the lexicographically largest one. Otherwise, output 1-1.

Two undirected graphs G1,G2G_1, G_2 are isomorphic if and only if there exists a permutation qq of 1n1 \sim n such that $\forall (u,v)\in G_1,(q_u,q_v)\in G_2,\forall (u,v)\notin G_1,(q_u,q_v)\notin G_2$.

Input Format

The first line contains an integer nn.

The next n1n-1 lines each contain two integers u,vu,v, indicating that there is an edge between uu and vv.

Output Format

Output one line with nn integers representing the answer, or output a single number 1-1 indicating that there is no solution.

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

Hint

For 100%100\% of the data, 1u,vn5×1031\le u,v\le n\le 5\times 10^3.

Score nn Special Property
Subtask1\operatorname{Subtask}1 1515 8\le 8 None
Subtask2\operatorname{Subtask}2 55 No special limit The tree degenerates into a chain
Subtask3\operatorname{Subtask}3 1515 The number of nodes with degree 3\ge 3 is 1\le 1
Subtask4\operatorname{Subtask}4 2020 100\le 100 None
Subtask5\operatorname{Subtask}5 103\le 10^3
Subtask6\operatorname{Subtask}6 2525 No special limit

Note: the node labels in the sample explanations are the indices in pp.

Sample Explanation 1

The shape of GpG_p is as follows:

It can be proven that there is no better solution.

Sample Explanation 2

The shape of GpG_p is as follows:

It can be proven that there is no better solution.

Translated by ChatGPT 5