#P8578. [CoE R5] So What Do We Do Now?

    ID: 9745 远端评测题 1500ms 256MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>搜索洛谷原创Special JudgeO2优化构造洛谷月赛

[CoE R5] So What Do We Do Now?

Background

396ac9d3c58dbf329d6ead206944a5a495930006.jpg

I’m getting tired of hiding.\texttt{I'm getting tired of hiding.}

Statement: The image above is taken from the Internet, pid:6544352. If there is any infringement, please inform us and it will be deleted.

Problem Description

Given a rooted tree with nn nodes, where the root is node 11. Let the subtree rooted at ii be TiT_i. You need to assign each node a positive integer weight viv_i such that all node weights are exactly 1,2,,n1,2,\dots,n, each used once.

Define

f=i=1nRi,f=\sum_{i=1}^{n}R_i,

where RiR_i is the range of weights in the subtree rooted at ii, i.e.

$$R_i=\max_{j \in T_i}\{v_j\}-\min_{j \in T_i}\{v_j\}.$$

Among all valid assignments, find one assignment of node weights that minimizes ff.

Input Format

The first line contains a positive integer nn, the number of nodes in the tree.

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

Output Format

Output one permutation of 1,2,,n1,2,\dots,n, representing the node weights of nodes 1,2,,n1,2,\dots,n. Since there may be many valid assignments, you only need to output any one of them.

3
1 2
1 3
1 2 3
2
1 2
1 2
5
1 2
2 3
3 4
4 5
1 2 3 4 5

Hint

Sample Explanation

Input #1\#1

graph.png

R1=31=2,R2=22=0,R3=33=0,f=R1+R2+R3=2R_1=3-1=2,R_2=2-2=0,R_3=3-3=0,f=R_1+R_2+R_3=2. It can be proven that there is no construction that makes ff smaller.


Constraints

For 10%10\% of the testdata, n10n \le 10.

For another 10%10\% of the testdata, the tree is a chain.

For another 20%20\% of the testdata, there is one node connected to the other n1n-1 nodes.

For another 20%20\% of the testdata, the tree is a complete binary tree, meaning every non-leaf node has exactly two children.

For 100%100\% of the testdata, 1n1061 \le n \le 10^6.

Translated by ChatGPT 5