結果

問題 No.277 根掘り葉掘り
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2017-03-12 12:13:54
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,100 ms / 3,000 ms
コード長 865 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 116,176 KB
最終ジャッジ日時 2023-09-12 11:53:01
合計ジャッジ時間 9,552 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,256 KB
testcase_01 AC 72 ms
71,136 KB
testcase_02 AC 73 ms
71,252 KB
testcase_03 AC 75 ms
71,456 KB
testcase_04 AC 75 ms
71,376 KB
testcase_05 AC 74 ms
71,408 KB
testcase_06 AC 75 ms
71,172 KB
testcase_07 AC 73 ms
71,132 KB
testcase_08 AC 72 ms
71,152 KB
testcase_09 AC 271 ms
101,072 KB
testcase_10 AC 1,100 ms
116,176 KB
testcase_11 AC 580 ms
103,420 KB
testcase_12 AC 739 ms
107,688 KB
testcase_13 AC 661 ms
107,504 KB
testcase_14 AC 680 ms
106,492 KB
testcase_15 AC 572 ms
103,108 KB
testcase_16 AC 618 ms
104,396 KB
testcase_17 AC 606 ms
104,896 KB
testcase_18 AC 571 ms
104,116 KB
testcase_19 AC 606 ms
103,984 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 = []
next_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
        next_queue.append(t)
        checked_nodes[t] = True
        node_lengths[t] = next_root_length
    if len(queue) == 0:
        if len(next_queue) == 0:
            break
        queue = next_queue
        next_queue = []

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