結果

問題 No.277 根掘り葉掘り
ユーザー matsu7874
提出日時 2015-10-04 17:18:02
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 804 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 45,604 KB
最終ジャッジ日時 2024-07-19 19:32:03
合計ジャッジ時間 7,669 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

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