結果

問題 No.277 根掘り葉掘り
ユーザー 👑 terasaterasa
提出日時 2023-01-15 13:02:12
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 459 ms / 3,000 ms
コード長 1,372 bytes
コンパイル時間 252 ms
使用メモリ 110,464 KB
最終ジャッジ日時 2023-01-15 13:02:21
合計ジャッジ時間 8,328 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 180 ms
85,916 KB
testcase_01 AC 185 ms
85,824 KB
testcase_02 AC 189 ms
86,104 KB
testcase_03 AC 181 ms
85,880 KB
testcase_04 AC 180 ms
86,092 KB
testcase_05 AC 181 ms
85,984 KB
testcase_06 AC 181 ms
86,100 KB
testcase_07 AC 179 ms
85,988 KB
testcase_08 AC 187 ms
86,000 KB
testcase_09 AC 406 ms
107,856 KB
testcase_10 AC 355 ms
110,464 KB
testcase_11 AC 416 ms
102,268 KB
testcase_12 AC 439 ms
104,452 KB
testcase_13 AC 449 ms
103,108 KB
testcase_14 AC 420 ms
102,940 KB
testcase_15 AC 459 ms
102,388 KB
testcase_16 AC 421 ms
102,696 KB
testcase_17 AC 418 ms
102,684 KB
testcase_18 AC 453 ms
102,792 KB
testcase_19 AC 418 ms
102,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Tuple, Callable, TypeVar, Optional
import sys
import itertools
import heapq
import bisect
import math
from collections import deque, defaultdict
from functools import lru_cache, cmp_to_key

input = sys.stdin.readline

if __file__ != 'prog.py':
    sys.setrecursionlimit(10 ** 6)


def readints(): return map(int, input().split())
def readlist(): return list(readints())
def readstr(): return input()[:-1]
def readlist1(): return list(map(lambda x: int(x) - 1, input().split()))


N = int(input())
E = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = readints()
    u -= 1
    v -= 1
    E[u].append(v)
    E[v].append(u)

R = [0] * N
is_leaf = [True] * N
stack = [(0, -1)]
while stack:
    v, p = stack.pop()
    if v < 0:
        v = ~v
        for dst in E[v]:
            if dst == p:
                continue
            is_leaf[v] = False
    else:
        if ~p:
            R[v] = R[p] + 1
        stack.append((~v, p))
        for dst in E[v]:
            if dst == p:
                continue
            stack.append((dst, v))

INF = 1 << 30
L = [INF] * N
dq = deque()
for i in range(N):
    if is_leaf[i]:
        L[i] = 0
        dq.append(i)
while dq:
    v = dq.popleft()
    for dst in E[v]:
        if L[dst] > L[v] + 1:
            L[dst] = L[v] + 1
            dq.append(dst)

for r, l in zip(R, L):
    print(min(r, l))
0