結果

問題 No.277 根掘り葉掘り
ユーザー konsin_tokagekonsin_tokage
提出日時 2018-03-13 08:52:48
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 477 ms / 3,000 ms
コード長 590 bytes
コンパイル時間 540 ms
使用メモリ 111,808 KB
最終ジャッジ日時 2022-12-18 10:26:35
合計ジャッジ時間 8,827 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 82 ms
75,516 KB
testcase_01 AC 83 ms
75,444 KB
testcase_02 AC 85 ms
75,836 KB
testcase_03 AC 87 ms
75,588 KB
testcase_04 AC 86 ms
75,696 KB
testcase_05 AC 86 ms
75,860 KB
testcase_06 AC 88 ms
75,772 KB
testcase_07 AC 87 ms
75,752 KB
testcase_08 AC 83 ms
75,496 KB
testcase_09 AC 404 ms
105,772 KB
testcase_10 AC 368 ms
111,808 KB
testcase_11 AC 444 ms
108,812 KB
testcase_12 AC 431 ms
107,088 KB
testcase_13 AC 477 ms
109,352 KB
testcase_14 AC 444 ms
109,476 KB
testcase_15 AC 438 ms
108,324 KB
testcase_16 AC 439 ms
108,720 KB
testcase_17 AC 436 ms
108,552 KB
testcase_18 AC 431 ms
108,512 KB
testcase_19 AC 435 ms
109,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    graph[x].append(y)
    graph[y].append(x)
R = [n]*n
R[0] = 0
nodes = [0]
for i in nodes:
    for j in graph[i]:
        if R[j] == n:
            R[j] = R[i]+1
            nodes.append(j)
L = [n]*n
leafs = []
for i in range(1, n):
    if len(graph[i]) == 1:
        L[i] = 0
        leafs.append(i)
for i in leafs:
    for j in graph[i]:
        if L[j] == n:
            leafs.append(j)
        L[j] = min(L[j], L[i]+1)
for i in map(min, R, L):
    print(i)
0