問題一覧 > 通常問題

No.277 Digging Up Roots and Leaves

レベル : / AI推定難易度 : 900 / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 159
作問者 : koyumeishi
お気に入りにしたユーザー ProblemId : 666 / 自分の提出
問題文最終更新日: 2026-06-29 23:14:39
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 120 (順位表) の他の問題:

Problem Statement

You are given a tree with $N$ vertices.
Each vertex is assigned a distinct number from $1$ to $N$, and vertex $1$ is the root of this tree.
For a vertex $i$, let $R_i$ be its distance from the root, and $L_i$ be its distance from the nearest leaf.
Output $min(R_i, L_i)$ in order of vertex number.

Note: The minimum number of edges traversed when moving from vertex $u$ to vertex $v$ is defined as the distance from vertex $u$ to vertex $v$ (the distance from vertex $v$ to vertex $u$).

Input

$N$
$x_1$ $y_1$
$\vdots$
$x_{N-1}$ $y_{N-1}$

The first line contains the number of vertices $N$.
The following $N-1$ lines contain the edge information, separated by spaces. The $i$-th edge connects vertices $x_i$ and $y_i$.
All inputs are integers and satisfy the following constraints.
$ 2 \leq N \leq 10^5$
$ 1 \leq x_i < y_i \leq N$
It is guaranteed that $ x_i \neq x_j $ or $ y_i \neq y_j $ when $ i \neq j $. That is, there are no multiple edges or self-loops, and the tree is connected.

Output

$min(R_1, L_1)$
$\vdots$
$min(R_N, L_N)$

Output $min(R_i, L_i)$ on the $i$-th line.

Sample

Sample 1
Input
6
1 2
1 3
2 4
2 5
5 6
Output
0
1
0
0
1
0

$R_i$ is shown in blue and $L_i$ in red.

Sample 2
Input
6
1 2
2 3
3 4
4 5
5 6
Output
0
1
2
2
1
0

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。