結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,916 KB
testcase_01 AC 16 ms
7,832 KB
testcase_02 AC 16 ms
8,008 KB
testcase_03 AC 17 ms
7,864 KB
testcase_04 AC 16 ms
7,864 KB
testcase_05 AC 16 ms
7,872 KB
testcase_06 AC 16 ms
7,868 KB
testcase_07 AC 15 ms
7,812 KB
testcase_08 AC 16 ms
7,892 KB
testcase_09 AC 579 ms
35,660 KB
testcase_10 AC 1,330 ms
36,608 KB
testcase_11 AC 879 ms
38,672 KB
testcase_12 AC 1,002 ms
38,736 KB
testcase_13 AC 958 ms
39,016 KB
testcase_14 AC 968 ms
39,020 KB
testcase_15 AC 856 ms
38,948 KB
testcase_16 AC 888 ms
38,804 KB
testcase_17 AC 878 ms
38,984 KB
testcase_18 AC 854 ms
38,832 KB
testcase_19 AC 854 ms
39,044 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