結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
6,272 KB
testcase_01 AC 13 ms
6,264 KB
testcase_02 AC 13 ms
6,236 KB
testcase_03 AC 15 ms
6,548 KB
testcase_04 AC 14 ms
6,416 KB
testcase_05 AC 14 ms
6,312 KB
testcase_06 AC 14 ms
6,512 KB
testcase_07 AC 14 ms
6,488 KB
testcase_08 AC 14 ms
6,416 KB
testcase_09 AC 1,251 ms
143,860 KB
testcase_10 AC 714 ms
50,912 KB
testcase_11 AC 767 ms
52,372 KB
testcase_12 AC 1,004 ms
91,316 KB
testcase_13 AC 839 ms
52,692 KB
testcase_14 AC 801 ms
54,088 KB
testcase_15 AC 857 ms
56,580 KB
testcase_16 AC 811 ms
55,676 KB
testcase_17 AC 781 ms
54,400 KB
testcase_18 AC 786 ms
54,760 KB
testcase_19 AC 777 ms
54,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python
from collections import defaultdict
import resource, sys
if sys.version_info[0]>=3: raw_input=input

resource.setrlimit(resource.RLIMIT_STACK, (2**29,-1))
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