結果

問題 No.277 根掘り葉掘り
ユーザー yuppe19 😺yuppe19 😺
提出日時 2015-09-06 17:39:21
言語 Python2
(2.7.18)
結果
AC  
実行時間 570 ms / 3,000 ms
コード長 542 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 6,588 KB
実行使用メモリ 33,528 KB
最終ジャッジ日時 2023-09-26 09:36:18
合計ジャッジ時間 7,708 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,180 KB
testcase_01 AC 13 ms
6,224 KB
testcase_02 AC 12 ms
6,200 KB
testcase_03 AC 13 ms
6,300 KB
testcase_04 AC 12 ms
6,196 KB
testcase_05 AC 13 ms
6,184 KB
testcase_06 AC 13 ms
6,196 KB
testcase_07 AC 13 ms
6,300 KB
testcase_08 AC 13 ms
6,280 KB
testcase_09 AC 538 ms
33,528 KB
testcase_10 AC 483 ms
27,600 KB
testcase_11 AC 547 ms
27,600 KB
testcase_12 AC 551 ms
27,684 KB
testcase_13 AC 555 ms
27,992 KB
testcase_14 AC 532 ms
28,404 KB
testcase_15 AC 531 ms
28,632 KB
testcase_16 AC 546 ms
28,192 KB
testcase_17 AC 540 ms
28,460 KB
testcase_18 AC 536 ms
28,608 KB
testcase_19 AC 570 ms
28,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python
from collections import deque

n = int(raw_input())
G = [[] for _ in xrange(n)]
for _ in xrange(n-1):
    x, y = map(lambda p: int(p)-1, raw_input().split())
    G[x].append(y)
    G[y].append(x)

que = deque([0])
dist = [0] + [float('inf')] * (n - 1)
for v in xrange(1, n):
    if len(G[v]) == 1:
        dist[v] = 0
        que.append(v)

while que:
    v = que.popleft()
    for vv in G[v]:
        if dist[vv] > dist[v] + 1:
            dist[vv] = dist[v] + 1
            que.append(vv)

print '\n'.join(map(str, dist))
0