結果

問題 No.277 根掘り葉掘り
ユーザー konsin_tokagekonsin_tokage
提出日時 2018-03-13 08:52:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 336 ms / 3,000 ms
コード長 590 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 102,656 KB
最終ジャッジ日時 2024-04-28 23:23:50
合計ジャッジ時間 6,225 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
51,968 KB
testcase_01 AC 46 ms
51,968 KB
testcase_02 AC 43 ms
51,840 KB
testcase_03 AC 46 ms
52,864 KB
testcase_04 AC 48 ms
52,736 KB
testcase_05 AC 51 ms
52,736 KB
testcase_06 AC 47 ms
52,736 KB
testcase_07 AC 53 ms
52,608 KB
testcase_08 AC 49 ms
51,840 KB
testcase_09 AC 301 ms
101,504 KB
testcase_10 AC 253 ms
98,092 KB
testcase_11 AC 307 ms
102,016 KB
testcase_12 AC 295 ms
101,504 KB
testcase_13 AC 336 ms
102,656 KB
testcase_14 AC 321 ms
102,656 KB
testcase_15 AC 307 ms
102,272 KB
testcase_16 AC 320 ms
102,400 KB
testcase_17 AC 303 ms
102,272 KB
testcase_18 AC 323 ms
102,016 KB
testcase_19 AC 322 ms
102,144 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