結果

問題 No.277 根掘り葉掘り
ユーザー matsu7874matsu7874
提出日時 2015-10-04 17:23:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 635 ms / 3,000 ms
コード長 529 bytes
コンパイル時間 559 ms
コンパイル使用メモリ 10,812 KB
実行使用メモリ 28,748 KB
最終ジャッジ日時 2023-09-27 01:58:34
合計ジャッジ時間 8,412 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,428 KB
testcase_01 AC 21 ms
8,496 KB
testcase_02 AC 19 ms
8,580 KB
testcase_03 AC 19 ms
8,460 KB
testcase_04 AC 23 ms
8,568 KB
testcase_05 AC 20 ms
8,464 KB
testcase_06 AC 21 ms
8,456 KB
testcase_07 AC 19 ms
8,520 KB
testcase_08 AC 19 ms
8,444 KB
testcase_09 AC 594 ms
28,748 KB
testcase_10 AC 533 ms
27,164 KB
testcase_11 AC 587 ms
27,620 KB
testcase_12 AC 617 ms
27,552 KB
testcase_13 AC 635 ms
27,828 KB
testcase_14 AC 607 ms
28,240 KB
testcase_15 AC 592 ms
27,692 KB
testcase_16 AC 605 ms
27,664 KB
testcase_17 AC 620 ms
27,900 KB
testcase_18 AC 586 ms
27,492 KB
testcase_19 AC 584 ms
27,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

N = int(input())
tree = [[] for i in range(N)]
for i in range(N - 1):
    x, y = map(int, input().split())
    tree[x - 1].append(y - 1)
    tree[y - 1].append(x - 1)

L = [N * 2] * N
dq = collections.deque()

L[0] = 0
dq.append(0)

for i in range(N):
    if len(tree[i]) == 1:
        L[i] = 0
        dq.append(i)

while dq:
    cur = dq.popleft()
    for t in tree[cur]:
        if L[t] <= L[cur] + 1:
            continue
        L[t] = L[cur] + 1
        dq.append(t)

for i in range(N):
    print(L[i])
0