結果

問題 No.277 根掘り葉掘り
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2015-09-09 09:25:20
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 653 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 11,364 KB
実行使用メモリ 19,568 KB
最終ジャッジ日時 2023-09-26 10:12:49
合計ジャッジ時間 5,759 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
19,568 KB
testcase_01 AC 81 ms
15,044 KB
testcase_02 AC 81 ms
15,152 KB
testcase_03 AC 84 ms
15,228 KB
testcase_04 AC 82 ms
15,044 KB
testcase_05 AC 83 ms
15,048 KB
testcase_06 AC 84 ms
15,324 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:8: warning: assigned but unused variable - from
Main.rb:8: warning: assigned but unused variable - to
Syntax OK

ソースコード

diff #

# Here your code !
INF = 10 ** 8
n = gets.to_i
P = Array.new(n+1,1); P[0] = 1; 
R = Array.new(n+1,INF); R[0] = 0; R[1] = 0
L = Array.new(n+1,INF); L[0] = 0;

E = (n - 1).times.map { from, to = gets.split.map(&:to_i) }.group_by {|from,to| from }
(1..n).each do |from|
    if E[from]
        E[from].each do |(_,to)|
            P[to] = from
            R[to] = R[from] + 1
        end
    end
end

leaf = (1..n).reject {|v| E[v] }
leaf.each do |i|
    d = R[i] + 1
    d.times.inject(i) do |j, dep|
        L[j] = [L[j], dep].min
        P[j]
    end
end

(1..n).each do |i|
  L[i] = [ L[i], L[P[i]] + 1].min
end

puts (1..n).map {|i| [R[i], L[i]].min }
0