結果

問題 No.277 根掘り葉掘り
ユーザー しらっ亭しらっ亭
提出日時 2015-09-05 20:50:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,003 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 36,300 KB
最終ジャッジ日時 2023-09-26 08:56:01
合計ジャッジ時間 7,251 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,480 KB
testcase_01 AC 18 ms
8,684 KB
testcase_02 AC 19 ms
8,680 KB
testcase_03 AC 20 ms
8,500 KB
testcase_04 AC 19 ms
8,504 KB
testcase_05 AC 20 ms
8,660 KB
testcase_06 AC 19 ms
8,696 KB
testcase_07 WA -
testcase_08 AC 19 ms
8,584 KB
testcase_09 WA -
testcase_10 AC 468 ms
36,120 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


def solve():
    N = int(input())

    C = defaultdict(list)
    P = [0] * (N + 1)
    for i in range(N - 1):
        x, y = map(int, input().split())
        C[x].append(y)
        P[y] = x

    R = [0] * (N + 1)
    stack = [(1, 0)]
    while stack:
        v, r = stack.pop()
        R[v] = r
        if C[v]:
            for c in C[v]:
                stack.append((c, r+1))

    L = [float('inf')] * (N + 1)
    s = set()
    for i in range(N + 1):
        if not C[i]:
            L[i] = 0
            s.add(i)

    def upd(x, d):
        if L[x] > d:
            L[x] = d
            return True
        return False

    d = 1
    while s:
        ns = set()
        for i in s:
            if upd(P[i], d):
                ns.add(P[i])
            for j in C[i]:
                if upd(j, d):
                    ns.add(j)
        s = ns
        d += 1

    for i in range(1, N + 1):
        print(min(R[i], L[i]))


if __name__ == '__main__':
    solve()
0