結果

問題 No.277 根掘り葉掘り
ユーザー cielciel
提出日時 2015-09-05 01:49:14
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 1,081 ms / 3,000 ms
コード長 648 bytes
コンパイル時間 785 ms
コンパイル使用メモリ 77,820 KB
実行使用メモリ 399,924 KB
最終ジャッジ日時 2023-09-26 08:29:46
合計ジャッジ時間 10,756 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
78,292 KB
testcase_01 AC 84 ms
78,204 KB
testcase_02 AC 83 ms
78,240 KB
testcase_03 AC 86 ms
78,020 KB
testcase_04 AC 85 ms
78,244 KB
testcase_05 AC 84 ms
78,292 KB
testcase_06 AC 85 ms
78,040 KB
testcase_07 AC 85 ms
77,952 KB
testcase_08 AC 83 ms
78,464 KB
testcase_09 AC 863 ms
233,116 KB
testcase_10 AC 336 ms
117,744 KB
testcase_11 AC 503 ms
117,920 KB
testcase_12 AC 744 ms
173,272 KB
testcase_13 AC 617 ms
117,356 KB
testcase_14 AC 1,081 ms
399,924 KB
testcase_15 AC 1,048 ms
247,040 KB
testcase_16 AC 750 ms
266,036 KB
testcase_17 AC 701 ms
191,072 KB
testcase_18 AC 734 ms
221,828 KB
testcase_19 AC 551 ms
130,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python
from collections import defaultdict
import sys
if sys.version_info[0]>=3: raw_input=input
sys.setrecursionlimit(10**6)

def dfs(n,d,z):
	r=None
	for e in H[n]:
		if e not in z:
			z[e]=1
			if r is None: r=1<<29
			x=dfs(e,d+1,z)
			r=min(r,x+1)
			del z[e]
	if r is None: r=0
	R[n]=min(r,d)
	L[n]=r
	return r

def dfs2(n,d,z):
	for e in H[n]:
		if e not in z:
			z[e]=1
			dfs2(e,min(d,L[n])+1,z)
			del z[e]
	R[n]=min(R[n],d)

n=int(raw_input())
H=defaultdict(list)
R={}
L={}
for _ in range(n-1):
	x,y=map(int,raw_input().split())
	H[x].append(y)
	H[y].append(x)

dfs(1,0,{1:1})
dfs2(1,0,{1:1})
for i in range(1,n+1):print(R[i])
0