結果

問題 No.277 根掘り葉掘り
ユーザー terasa
提出日時 2023-01-15 12:53:51
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,210 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 101,112 KB
最終ジャッジ日時 2024-12-28 03:23:25
合計ジャッジ時間 5,262 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 WA * 8
権限があれば一括ダウンロードができます

ソースコード

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
L = [N] * N
stack = [(0, -1)]
while stack:
    v, p = stack.pop()
    if v < 0:
        v = ~v
        is_leaf = True
        for dst in E[v]:
            if dst == p:
                continue
            is_leaf = False
            L[v] = min(L[v], L[dst] + 1)
        if is_leaf:
            L[v] = 0
    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))

for i in range(N):
    print(min(R[i], L[i]))
0