結果

問題 No.806 木を道に
ユーザー simansiman
提出日時 2020-09-24 14:00:04
言語 Ruby
(3.2.2)
結果
WA  
実行時間 -
コード長 334 bytes
コンパイル時間 65 ms
コンパイル使用メモリ 11,340 KB
実行使用メモリ 27,944 KB
最終ジャッジ日時 2023-09-10 13:25:50
合計ジャッジ時間 8,121 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,092 KB
testcase_01 AC 82 ms
14,996 KB
testcase_02 AC 80 ms
15,012 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 79 ms
15,232 KB
testcase_09 AC 81 ms
15,032 KB
testcase_10 AC 140 ms
17,648 KB
testcase_11 AC 131 ms
17,196 KB
testcase_12 WA -
testcase_13 AC 257 ms
22,680 KB
testcase_14 AC 327 ms
26,292 KB
testcase_15 AC 354 ms
26,364 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 140 ms
17,712 KB
testcase_20 AC 301 ms
25,320 KB
testcase_21 AC 194 ms
20,144 KB
testcase_22 AC 378 ms
27,872 KB
testcase_23 AC 383 ms
27,944 KB
testcase_24 AC 195 ms
21,012 KB
testcase_25 AC 262 ms
24,208 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 = [*1..N].index { |u| E[u].size == 1 }
f[root]

puts ans
0