結果

問題 No.277 根掘り葉掘り
ユーザー cielciel
提出日時 2015-09-05 01:35:53
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 493 bytes
コンパイル時間 503 ms
コンパイル使用メモリ 11,012 KB
実行使用メモリ 43,112 KB
最終ジャッジ日時 2023-09-26 08:15:34
合計ジャッジ時間 9,913 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,276 KB
testcase_01 AC 82 ms
15,104 KB
testcase_02 AC 85 ms
15,128 KB
testcase_03 AC 84 ms
15,044 KB
testcase_04 AC 82 ms
15,284 KB
testcase_05 AC 83 ms
15,068 KB
testcase_06 AC 86 ms
15,120 KB
testcase_07 AC 85 ms
15,148 KB
testcase_08 AC 83 ms
15,108 KB
testcase_09 RE -
testcase_10 AC 866 ms
39,400 KB
testcase_11 AC 921 ms
34,584 KB
testcase_12 RE -
testcase_13 AC 948 ms
35,184 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 916 ms
43,112 KB
testcase_19 AC 941 ms
41,716 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/ruby
def dfs(n,d,z)
	r=nil
	H[n].each{|e|
		if !z.has_key?(e)
			z[e]=1
			r||=1<<29
			x=dfs(e,d+1,z)
			r=[r,x+1].min
			z.delete(e)
		end
	}
	r||=0
	R[n]=[r,d].min
	L[n]=r
	r
end
def dfs2(n,d,z)
	H[n].each{|e|
		if !z.has_key?(e)
			z[e]=1
			dfs2(e,[d,L[n]].min+1,z)
			z.delete(e)
		end
	}
	R[n]=[R[n],d].min
end
		
n=gets.to_i
H=Hash.new{|h,k|h[k]=[]}
R={}
L={}
(n-1).times{
	x,y=gets.split.map(&:to_i)
	H[x]<<y
	H[y]<<x
}
dfs(1,0,{1=>1})
dfs2(1,0,{1=>1})
1.step(n){|i|p R[i]}
0