結果

問題 No.277 根掘り葉掘り
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-05 18:08:47
言語 Python3
(3.10.1 + numpy 1.22.3 + scipy 1.8.0)
結果
AC  
実行時間 695 ms / 3,000 ms
コード長 851 bytes
コンパイル時間 380 ms
使用メモリ 43,132 KB
最終ジャッジ日時 2023-02-17 06:07:53
合計ジャッジ時間 9,373 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 17 ms
7,848 KB
testcase_01 AC 16 ms
9,820 KB
testcase_02 AC 16 ms
9,760 KB
testcase_03 AC 17 ms
9,764 KB
testcase_04 AC 16 ms
7,804 KB
testcase_05 AC 17 ms
9,756 KB
testcase_06 AC 16 ms
9,688 KB
testcase_07 AC 17 ms
7,632 KB
testcase_08 AC 16 ms
9,736 KB
testcase_09 AC 661 ms
39,400 KB
testcase_10 AC 569 ms
42,540 KB
testcase_11 AC 660 ms
40,008 KB
testcase_12 AC 656 ms
40,152 KB
testcase_13 AC 695 ms
43,132 KB
testcase_14 AC 654 ms
42,952 KB
testcase_15 AC 645 ms
42,308 KB
testcase_16 AC 637 ms
41,676 KB
testcase_17 AC 634 ms
42,840 KB
testcase_18 AC 634 ms
41,540 KB
testcase_19 AC 635 ms
42,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N = int(input())
    Es = [set() for n in range(N)]
    for n in range(N-1):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        Es[x].add(y)
        Es[y].add(x)
    return N, Es

def solve(N, Es):
    # 根と葉を始点とした幅優先探索で、距離の近いところから埋めていく。
    frontier = [0] + [v for v in range(1, N) if len(Es[v]) == 1]
    dist = [N] * N
    for pos in frontier:
        dist[pos] = 0
    d = 0
    while frontier:
        d += 1
        new_frontier = []
        for pos in frontier:
            for n_pos in Es[pos]:
                if dist[n_pos] > d:
                    dist[n_pos] = d
                    new_frontier.append(n_pos)
        frontier = new_frontier
    return dist

N, Es = read_data()
dist = solve(N, Es)
for d in dist:
    print(d)
0