結果

問題 No.277 根掘り葉掘り
ユーザー konsin_tokagekonsin_tokage
提出日時 2018-03-13 08:52:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 336 ms / 3,000 ms
コード長 590 bytes
コンパイル時間 1,585 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 104,596 KB
最終ジャッジ日時 2023-08-11 06:43:49
合計ジャッジ時間 6,910 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,556 KB
testcase_01 AC 77 ms
71,448 KB
testcase_02 AC 74 ms
71,276 KB
testcase_03 AC 75 ms
71,208 KB
testcase_04 AC 75 ms
71,372 KB
testcase_05 AC 75 ms
71,120 KB
testcase_06 AC 75 ms
71,124 KB
testcase_07 AC 75 ms
71,348 KB
testcase_08 AC 75 ms
71,392 KB
testcase_09 AC 278 ms
102,436 KB
testcase_10 AC 252 ms
104,596 KB
testcase_11 AC 316 ms
103,124 KB
testcase_12 AC 299 ms
102,332 KB
testcase_13 AC 336 ms
103,900 KB
testcase_14 AC 310 ms
103,052 KB
testcase_15 AC 308 ms
103,540 KB
testcase_16 AC 315 ms
103,404 KB
testcase_17 AC 304 ms
102,600 KB
testcase_18 AC 304 ms
103,108 KB
testcase_19 AC 308 ms
103,572 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