結果

問題 No.277 根掘り葉掘り
コンテスト
ユーザー matsu7874
提出日時 2015-10-04 17:18:02
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
TLE  
実行時間 -
コード長 804 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 461 ms
コンパイル使用メモリ 20,732 KB
実行使用メモリ 48,380 KB
最終ジャッジ日時 2026-04-02 18:03:51
合計ジャッジ時間 8,358 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import collections
N = int(input())
tree = [[] for i in range(N)]
for i in range(N - 1):
    x, y = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

R = [N * 2] * N
L = [N * 2] * N

dq = collections.deque()
R[0] = 0
dq.append(0)
while dq:
    cur = dq.popleft()
    for t in tree[cur]:
        if R[t] < R[cur] + 1:
            continue
        R[t] = R[cur] + 1
        dq.append(t)

leaf = []
for i in range(N):
    if len(tree[i]) == 1:
        L[i] = 0
        leaf.append((R[i], i))
leaf.sort()
for r, l in leaf:
    dq.append(l)
    while dq:
        cur = dq.popleft()
        for t in tree[cur]:
            if L[t] <= L[cur] + 1:
                continue
            L[t] = L[cur] + 1
            dq.append(t)

for i in range(N):
    print(min(R[i], L[i]))
0