結果

問題 No.277 根掘り葉掘り
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2017-03-12 12:34:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 631 ms / 3,000 ms
コード長 795 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 10,860 KB
実行使用メモリ 39,396 KB
最終ジャッジ日時 2023-09-12 11:53:55
合計ジャッジ時間 8,540 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,560 KB
testcase_01 AC 19 ms
8,424 KB
testcase_02 AC 20 ms
8,556 KB
testcase_03 AC 20 ms
8,560 KB
testcase_04 AC 19 ms
8,704 KB
testcase_05 AC 19 ms
8,580 KB
testcase_06 AC 19 ms
8,716 KB
testcase_07 AC 19 ms
8,524 KB
testcase_08 AC 18 ms
8,596 KB
testcase_09 AC 602 ms
35,928 KB
testcase_10 AC 519 ms
36,804 KB
testcase_11 AC 611 ms
38,980 KB
testcase_12 AC 616 ms
38,900 KB
testcase_13 AC 631 ms
39,340 KB
testcase_14 AC 603 ms
39,396 KB
testcase_15 AC 604 ms
39,196 KB
testcase_16 AC 612 ms
39,260 KB
testcase_17 AC 600 ms
39,132 KB
testcase_18 AC 605 ms
39,048 KB
testcase_19 AC 598 ms
39,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n = int(input())
edges = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    x, y = [int(i) for i in input().split()]
    edges[x].append(y)
    edges[y].append(x)

zero_nodes = [1] + [i for i, v in list(enumerate(edges))[2:] if len(v) == 1]

node_lengths = [None] * (n + 1)
checked_nodes = [False] * (n + 1)

queue = deque()

for z in zero_nodes:
    queue.append(z)
    checked_nodes[z] = True
    node_lengths[z] = 0

while True:
    i = queue.popleft()
    next_root_length = node_lengths[i] + 1
    for t in edges[i]:
        if checked_nodes[t]:
            continue
        queue.append(t)
        checked_nodes[t] = True
        node_lengths[t] = next_root_length
    if len(queue) == 0:
        break

node_lengths.pop(0)
print(*node_lengths, sep='\n')
0