結果

問題 No.277 根掘り葉掘り
ユーザー ああいいああいい
提出日時 2022-03-17 23:03:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 281 ms / 3,000 ms
コード長 665 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 82,436 KB
実行使用メモリ 99,384 KB
最終ジャッジ日時 2024-04-09 21:39:05
合計ジャッジ時間 4,986 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,860 KB
testcase_01 AC 40 ms
54,156 KB
testcase_02 AC 39 ms
55,256 KB
testcase_03 AC 43 ms
55,408 KB
testcase_04 AC 45 ms
55,604 KB
testcase_05 AC 42 ms
55,636 KB
testcase_06 AC 41 ms
55,112 KB
testcase_07 AC 42 ms
56,080 KB
testcase_08 AC 43 ms
54,756 KB
testcase_09 AC 235 ms
90,224 KB
testcase_10 AC 220 ms
99,384 KB
testcase_11 AC 256 ms
89,832 KB
testcase_12 AC 234 ms
90,204 KB
testcase_13 AC 281 ms
90,716 KB
testcase_14 AC 249 ms
90,736 KB
testcase_15 AC 243 ms
90,000 KB
testcase_16 AC 254 ms
90,040 KB
testcase_17 AC 243 ms
90,144 KB
testcase_18 AC 248 ms
90,288 KB
testcase_19 AC 245 ms
90,284 KB
権限があれば一括ダウンロードができます

ソースコード

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 = [-5] * (N +1)
stack = [(1,0)]
from collections import deque
q = deque()
while stack:
    now,parent = stack.pop()
    if now != 0 and len(G[now]) == 1:
        q.append(now)
        dp[now] = 0
    for v in G[now]:
        if v == parent:continue
        depth[v] = depth[now] + 1
        stack.append((v,now))

while q:
    now = q.popleft()
    for v in G[now]:
        if dp[v] == -5:
            dp[v] = dp[now] + 1
            q.append(v)
for i in range(1,N+1):
    print(min(depth[i],dp[i]))
0