#P15780. [JAG 2025 Summer Camp #3] Communication between islands

[JAG 2025 Summer Camp #3] Communication between islands

题目描述

Solve the following problem for r=1,2,,Nr = 1, 2, \ldots, N.

There are NN islands, and with N1N - 1 bridges it is possible to travel between any two islands.

When there is an announcement to be made to all islands, flyers are distributed in a somewhat unusual way. First, exactly one flyer is created on island rr. After that, the following operation is repeated:

Choose one flyer, and let uu be the island that currently has it. Duplicate the flyer once, and deliver the original and the duplicate (exactly two flyers in total) to islands connected to uu by a bridge. These two flyers may both be delivered to the same island or to two different islands.

Determine the minimum number of operations required until every island has at least one flyer.

输入格式

The input consists of a single test case in the following format.

$$\begin{aligned} & N \\ & u_1 \ v_1 \\ & \vdots \\ & u_{N-1} \ v_{N-1} \end{aligned} $$

The first line contains an integer NN (2N3000002 \leq N \leq 300\,000), representing the number of islands.

The following N1N - 1 lines each contain integers ui,viu_i, v_i (1ui,viN1 \leq u_i, v_i \leq N, uiviu_i \neq v_i), representing that the ii-th bridge connects island uiu_i and island viv_i.

It is guaranteed that one can travel between any two islands using bridges.

输出格式

Output NN integers separated by spaces. The ii-th integer should contain the minimum number of operations when r=ir = i.

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