結果

問題 No.277 根掘り葉掘り
ユーザー cielciel
提出日時 2015-10-14 13:37:23
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 635 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 11,212 KB
実行使用メモリ 43,088 KB
最終ジャッジ日時 2023-09-28 13:06:24
合計ジャッジ時間 9,584 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
15,228 KB
testcase_01 AC 83 ms
15,304 KB
testcase_02 AC 84 ms
15,168 KB
testcase_03 AC 85 ms
15,152 KB
testcase_04 AC 86 ms
15,196 KB
testcase_05 AC 86 ms
15,304 KB
testcase_06 AC 86 ms
15,020 KB
testcase_07 AC 86 ms
15,224 KB
testcase_08 AC 84 ms
15,156 KB
testcase_09 RE -
testcase_10 AC 965 ms
39,440 KB
testcase_11 AC 990 ms
33,344 KB
testcase_12 RE -
testcase_13 AC 1,029 ms
35,308 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 991 ms
43,088 KB
testcase_19 AC 967 ms
41,788 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