結果

問題 No.277 根掘り葉掘り
ユーザー yuki2006yuki2006
提出日時 2015-09-05 02:44:32
言語 Python2
(2.7.18)
結果
AC  
実行時間 1,187 ms / 3,000 ms
コード長 550 bytes
コンパイル時間 1,134 ms
コンパイル使用メモリ 6,620 KB
実行使用メモリ 27,660 KB
最終ジャッジ日時 2023-09-26 08:33:05
合計ジャッジ時間 12,999 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,876 KB
testcase_01 AC 11 ms
5,872 KB
testcase_02 AC 11 ms
5,864 KB
testcase_03 AC 11 ms
5,888 KB
testcase_04 AC 12 ms
5,872 KB
testcase_05 AC 11 ms
6,008 KB
testcase_06 AC 12 ms
5,896 KB
testcase_07 AC 11 ms
6,028 KB
testcase_08 AC 11 ms
6,044 KB
testcase_09 AC 583 ms
27,260 KB
testcase_10 AC 471 ms
26,356 KB
testcase_11 AC 947 ms
26,284 KB
testcase_12 AC 1,160 ms
27,488 KB
testcase_13 AC 1,089 ms
26,744 KB
testcase_14 AC 958 ms
27,032 KB
testcase_15 AC 1,141 ms
27,436 KB
testcase_16 AC 1,187 ms
27,660 KB
testcase_17 AC 1,013 ms
27,512 KB
testcase_18 AC 1,095 ms
27,388 KB
testcase_19 AC 1,026 ms
27,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(raw_input())
edge = [[] for _ in xrange(N +1)]

for i in xrange(N - 1):
    x, y = map(int, raw_input().split())
    edge[x].append(y)
    edge[y].append(x)


queue = [1]
dist = [N] * (N + 1)
dist[1] = 0

for i in xrange(1, N + 1):
    if len(edge[i]) == 1:
        dist[i] = 0
        queue.append(i)

while len(queue) > 0:
    current = queue.pop()
    for nxt in edge[current]:
        if dist[nxt] > dist[current] + 1:
            dist[nxt] = dist[current] + 1
            queue.append(nxt)


for i in xrange(1, N + 1):
    print dist[i]
0