結果

問題 No.277 根掘り葉掘り
ユーザー rlangevinrlangevin
提出日時 2023-07-31 22:50:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,038 bytes
コンパイル時間 408 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 100,428 KB
最終ジャッジ日時 2024-04-18 22:09:50
合計ジャッジ時間 5,193 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,736 KB
testcase_01 AC 35 ms
52,224 KB
testcase_02 WA -
testcase_03 AC 34 ms
52,608 KB
testcase_04 AC 37 ms
52,480 KB
testcase_05 AC 37 ms
52,352 KB
testcase_06 AC 37 ms
52,608 KB
testcase_07 AC 43 ms
53,120 KB
testcase_08 AC 41 ms
52,608 KB
testcase_09 AC 162 ms
94,720 KB
testcase_10 AC 162 ms
100,428 KB
testcase_11 AC 185 ms
90,796 KB
testcase_12 AC 175 ms
93,492 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def non_rec_dfs(s, G):
    N = len(G)
    stack = []
    stack.append(s)
    par = [-1] * N
    depth = [-1] * N
    shallow = [10**18] * N
    sz = [1] * N
    depth[s] = 0
    while stack:
        u = stack.pop()
        if u >= 0:
            stack.append(~u)
            for v in G[u]:
                if v == par[u]:
                    continue
                par[v] = u
                depth[v] = depth[u] + 1
                stack.append(v)
        else:
            u = ~u
            if len(G[u]) == 1 and u != 0:
                shallow[u] = 0
            
            if u != 0:
                sz[par[u]] += sz[u]
                shallow[par[u]] = min(shallow[par[u]], shallow[u] + 1)
                        
    return depth, shallow


N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    G[u].append(v)
    G[v].append(u)

ans1, ans2 = non_rec_dfs(0, G)
for a, b in zip(ans1, ans2):
    print(min(a, b))
0