結果

問題 No.806 木を道に
ユーザー simansiman
提出日時 2020-09-24 14:03:05
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 334 bytes
コンパイル時間 519 ms
コンパイル使用メモリ 11,172 KB
実行使用メモリ 30,624 KB
最終ジャッジ日時 2023-09-10 13:25:59
合計ジャッジ時間 7,947 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,136 KB
testcase_01 AC 81 ms
15,240 KB
testcase_02 AC 82 ms
15,248 KB
testcase_03 AC 79 ms
15,176 KB
testcase_04 AC 81 ms
15,296 KB
testcase_05 AC 80 ms
15,176 KB
testcase_06 AC 81 ms
15,072 KB
testcase_07 AC 81 ms
15,124 KB
testcase_08 AC 81 ms
15,032 KB
testcase_09 AC 81 ms
15,212 KB
testcase_10 AC 137 ms
17,568 KB
testcase_11 AC 130 ms
17,392 KB
testcase_12 AC 363 ms
26,776 KB
testcase_13 AC 258 ms
22,568 KB
testcase_14 AC 322 ms
26,192 KB
testcase_15 AC 343 ms
26,344 KB
testcase_16 AC 165 ms
19,164 KB
testcase_17 AC 310 ms
25,792 KB
testcase_18 AC 101 ms
16,172 KB
testcase_19 AC 138 ms
17,408 KB
testcase_20 AC 308 ms
25,532 KB
testcase_21 AC 196 ms
20,224 KB
testcase_22 AC 381 ms
27,276 KB
testcase_23 AC 385 ms
27,692 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 157 ms
18,008 KB
testcase_27 AC 350 ms
25,932 KB
testcase_28 AC 86 ms
15,428 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
E = Hash.new { |h, k| h[k] = [] }

(N - 1).times do
  u, v = gets.split.map(&:to_i)
  E[u] << v
  E[v] << u
end

ans = 0

f =->(v, parent = -1) {
  cnt = E[v].size - 2
  ans += cnt if cnt > 0

  E[v].each do |u|
    next if u == parent

    f[u, v]
  end
}

root = [*0..N].index { |u| E[u].size == 1 }
f[root]

puts ans
0