結果

問題 No.277 根掘り葉掘り
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2017-03-12 12:13:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,116 ms / 3,000 ms
コード長 756 bytes
コンパイル時間 1,367 ms
コンパイル使用メモリ 87,200 KB
実行使用メモリ 116,080 KB
最終ジャッジ日時 2023-09-12 11:53:28
合計ジャッジ時間 11,383 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,560 KB
testcase_01 AC 76 ms
71,352 KB
testcase_02 AC 75 ms
71,256 KB
testcase_03 AC 76 ms
71,588 KB
testcase_04 AC 77 ms
71,432 KB
testcase_05 AC 76 ms
71,188 KB
testcase_06 AC 78 ms
71,248 KB
testcase_07 AC 78 ms
71,132 KB
testcase_08 AC 76 ms
71,136 KB
testcase_09 AC 301 ms
100,792 KB
testcase_10 AC 1,116 ms
116,080 KB
testcase_11 AC 842 ms
105,480 KB
testcase_12 AC 946 ms
104,972 KB
testcase_13 AC 952 ms
106,216 KB
testcase_14 AC 898 ms
107,916 KB
testcase_15 AC 735 ms
104,928 KB
testcase_16 AC 810 ms
105,520 KB
testcase_17 AC 764 ms
103,960 KB
testcase_18 AC 733 ms
105,504 KB
testcase_19 AC 737 ms
106,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 = []

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

while True:
    i = queue.pop(0)
    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