結果

問題 No.277 根掘り葉掘り
ユーザー cielciel
提出日時 2015-09-05 01:49:06
言語 Python2
(2.7.18)
結果
AC  
実行時間 1,226 ms / 3,000 ms
コード長 648 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 6,676 KB
実行使用メモリ 143,820 KB
最終ジャッジ日時 2023-09-26 08:28:07
合計ジャッジ時間 10,764 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,296 KB
testcase_01 AC 13 ms
6,244 KB
testcase_02 AC 13 ms
6,196 KB
testcase_03 AC 14 ms
6,316 KB
testcase_04 AC 13 ms
6,240 KB
testcase_05 AC 14 ms
6,292 KB
testcase_06 AC 14 ms
6,300 KB
testcase_07 AC 13 ms
6,272 KB
testcase_08 AC 13 ms
6,232 KB
testcase_09 AC 1,226 ms
143,820 KB
testcase_10 AC 693 ms
50,872 KB
testcase_11 AC 757 ms
52,364 KB
testcase_12 AC 992 ms
91,156 KB
testcase_13 AC 817 ms
52,804 KB
testcase_14 AC 785 ms
53,928 KB
testcase_15 AC 852 ms
56,700 KB
testcase_16 AC 793 ms
55,572 KB
testcase_17 AC 774 ms
54,328 KB
testcase_18 AC 772 ms
54,564 KB
testcase_19 AC 753 ms
54,280 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