結果

問題 No.277 根掘り葉掘り
ユーザー matsu7874matsu7874
提出日時 2015-10-04 17:12:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 785 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 37,396 KB
最終ジャッジ日時 2023-09-27 01:57:38
合計ジャッジ時間 6,089 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
37,396 KB
testcase_01 AC 19 ms
8,516 KB
testcase_02 AC 20 ms
8,616 KB
testcase_03 AC 20 ms
8,420 KB
testcase_04 AC 19 ms
8,500 KB
testcase_05 AC 19 ms
8,544 KB
testcase_06 AC 19 ms
8,496 KB
testcase_07 AC 19 ms
8,480 KB
testcase_08 AC 18 ms
8,532 KB
testcase_09 AC 769 ms
32,868 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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(i)

dq = collections.deque()
for r in leaf:
    dq.append(r)
    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