結果

問題 No.277 根掘り葉掘り
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2017-03-12 12:04:29
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 1,387 ms / 3,000 ms
コード長 756 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 38,964 KB
最終ジャッジ日時 2023-09-12 11:52:50
合計ジャッジ時間 13,750 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,796 KB
testcase_01 AC 16 ms
7,808 KB
testcase_02 AC 17 ms
7,856 KB
testcase_03 AC 17 ms
7,864 KB
testcase_04 AC 17 ms
7,852 KB
testcase_05 AC 17 ms
7,816 KB
testcase_06 AC 17 ms
7,784 KB
testcase_07 AC 17 ms
7,868 KB
testcase_08 AC 17 ms
7,792 KB
testcase_09 AC 618 ms
35,836 KB
testcase_10 AC 1,387 ms
36,444 KB
testcase_11 AC 1,103 ms
38,756 KB
testcase_12 AC 1,261 ms
38,636 KB
testcase_13 AC 1,213 ms
38,912 KB
testcase_14 AC 1,204 ms
38,964 KB
testcase_15 AC 993 ms
38,860 KB
testcase_16 AC 1,104 ms
38,768 KB
testcase_17 AC 1,052 ms
38,808 KB
testcase_18 AC 988 ms
38,872 KB
testcase_19 AC 1,034 ms
38,836 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