結果

問題 No.277 根掘り葉掘り
ユーザー terasaterasa
提出日時 2023-01-15 12:53:51
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,210 bytes
コンパイル時間 471 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 104,864 KB
最終ジャッジ日時 2023-08-27 20:32:47
合計ジャッジ時間 6,880 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 158 ms
80,148 KB
testcase_01 AC 159 ms
80,332 KB
testcase_02 WA -
testcase_03 AC 162 ms
80,160 KB
testcase_04 AC 161 ms
79,880 KB
testcase_05 AC 161 ms
80,244 KB
testcase_06 AC 158 ms
80,188 KB
testcase_07 AC 159 ms
80,240 KB
testcase_08 AC 157 ms
80,256 KB
testcase_09 AC 288 ms
103,524 KB
testcase_10 AC 260 ms
104,864 KB
testcase_11 AC 289 ms
95,512 KB
testcase_12 AC 280 ms
99,120 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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