結果

問題 No.277 根掘り葉掘り
ユーザー terasaterasa
提出日時 2023-01-15 13:02:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 352 ms / 3,000 ms
コード長 1,372 bytes
コンパイル時間 328 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 107,400 KB
最終ジャッジ日時 2023-08-27 20:39:47
合計ジャッジ時間 6,709 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
80,312 KB
testcase_01 AC 155 ms
80,352 KB
testcase_02 AC 158 ms
80,320 KB
testcase_03 AC 161 ms
79,996 KB
testcase_04 AC 162 ms
80,344 KB
testcase_05 AC 156 ms
80,208 KB
testcase_06 AC 159 ms
80,168 KB
testcase_07 AC 160 ms
80,316 KB
testcase_08 AC 158 ms
80,168 KB
testcase_09 AC 305 ms
102,812 KB
testcase_10 AC 285 ms
107,400 KB
testcase_11 AC 321 ms
96,372 KB
testcase_12 AC 311 ms
99,100 KB
testcase_13 AC 352 ms
97,220 KB
testcase_14 AC 333 ms
97,188 KB
testcase_15 AC 329 ms
96,788 KB
testcase_16 AC 326 ms
96,380 KB
testcase_17 AC 331 ms
97,104 KB
testcase_18 AC 331 ms
96,740 KB
testcase_19 AC 319 ms
96,620 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