結果

問題 No.277 根掘り葉掘り
ユーザー rpy3cpprpy3cpp
提出日時 2015-09-05 18:08:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 626 ms / 3,000 ms
コード長 851 bytes
コンパイル時間 1,097 ms
コンパイル使用メモリ 10,888 KB
実行使用メモリ 43,584 KB
最終ジャッジ日時 2023-09-26 08:50:56
合計ジャッジ時間 8,233 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,756 KB
testcase_01 AC 16 ms
7,776 KB
testcase_02 AC 16 ms
7,764 KB
testcase_03 AC 16 ms
7,928 KB
testcase_04 AC 16 ms
7,756 KB
testcase_05 AC 16 ms
7,868 KB
testcase_06 AC 16 ms
7,928 KB
testcase_07 AC 16 ms
7,776 KB
testcase_08 AC 16 ms
7,920 KB
testcase_09 AC 596 ms
39,728 KB
testcase_10 AC 510 ms
42,884 KB
testcase_11 AC 589 ms
40,376 KB
testcase_12 AC 583 ms
40,492 KB
testcase_13 AC 626 ms
43,584 KB
testcase_14 AC 603 ms
43,428 KB
testcase_15 AC 608 ms
42,660 KB
testcase_16 AC 602 ms
42,120 KB
testcase_17 AC 591 ms
43,084 KB
testcase_18 AC 584 ms
41,844 KB
testcase_19 AC 594 ms
42,920 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