結果

問題 No.277 根掘り葉掘り
ユーザー ああいいああいい
提出日時 2022-03-17 22:55:15
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 723 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 105,532 KB
最終ジャッジ日時 2024-04-09 21:29:18
合計ジャッジ時間 4,994 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,280 KB
testcase_01 AC 39 ms
53,012 KB
testcase_02 WA -
testcase_03 AC 39 ms
53,772 KB
testcase_04 AC 40 ms
53,844 KB
testcase_05 AC 39 ms
52,896 KB
testcase_06 AC 40 ms
52,820 KB
testcase_07 AC 39 ms
54,036 KB
testcase_08 AC 37 ms
52,504 KB
testcase_09 AC 247 ms
95,896 KB
testcase_10 AC 214 ms
105,532 KB
testcase_11 AC 247 ms
89,404 KB
testcase_12 AC 243 ms
92,164 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
G = [[] for _ in range(N+1)]
for _ in range(N-1):
    x,y = map(int,input().split())
    G[x].append(y)
    G[y].append(x)

depth = [0] * (N +1)
dp = [0] * (N +1)
stack = [(~1,0),(1,0)]
while stack:
    now,parent = stack.pop()
    if now >= 0:
        for v in G[now]:
            if v == parent:continue
            depth[v] = depth[now] + 1
            stack.append((~v,now))
            stack.append((v,now))
    else:
        now = ~now
        _min = 10 ** 6
        for v in G[now]:
            if v == parent:continue
            if dp[v] + 1 < _min:
                _min = dp[v] + 1
        if _min < 10 ** 6:
            dp[now] = _min
for i in range(1,N+1):
    print(min(depth[i],dp[i]))
    

0