#P15875. [ICPC 2026 NAC] Acorn Quarrels

[ICPC 2026 NAC] Acorn Quarrels

Description

There are NN squirrels storing acorns in a tree. The (botanical) oak tree is also a graph-theoretic tree: a connected graph with NN vertices labeled from 11 to NN and N1N-1 undirected edges. Each squirrel sits at a different vertex of the tree and two squirrels are neighbors\emph{neighbors} if their vertices share an edge.

In ascending order of vertex label starting with the squirrel at vertex 11, each squirrel steals one acorn from the neighboring squirrel that currently has the most acorns. If multiple neighbors are tied for having the most acorns, the squirrel steals one acorn from each of them!

To limit the fallout of these shenanigans, you want to distribute acorns to the squirrels so that each squirrel begins with at least NN acorns (so that no squirrel runs out of acorns due to thefts) and ends with the same number of acorns after all NN squirrels are done stealing from each other as they had originally. It can be shown that such a distribution exists where every squirrel begins with at most 2N12N-1 acorns. Find any distribution satisfying these conditions.

Input Format

The first line of input contains an integer NN (2N105)(2 \leq N \leq 10^5), the number of vertices (and squirrels).

Each of the following N1N-1 lines contains two space-separated integers uu and vv (1u,vN,uv)(1 \leq u, v \leq N, u \neq v), indicating that an edge exists between vertices uu and vv. There is at most one edge between any pair of vertices, and the edges form a tree.

Output Format

Print NN space-separated integers d1,d2,,dNd_1, d_2, \ldots, d_N satisfying Ndi2N1N \leq d_i \leq 2N-1, where did_i is the number of acorns you would like to distribute to the squirrel at vertex ii. Your solution will be accepted if every squirrel ends with the same number of acorns as they started with after all NN thefts have occurred. It can be proved that such a distribution of acorns always exists.

5
1 2
1 3
1 4
2 5
6 5 7 7 9
8
5 4
3 7
8 4
4 7
5 2
1 3
6 4
14 15 10 9 13 14 14 14