結果

問題 No.1637 Easy Tree Query
ユーザー simansiman
提出日時 2021-11-26 15:37:02
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 450 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 11,328 KB
実行使用メモリ 31,376 KB
最終ジャッジ日時 2023-09-11 22:42:23
合計ジャッジ時間 16,655 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,284 KB
testcase_01 AC 78 ms
15,076 KB
testcase_02 AC 605 ms
26,232 KB
testcase_03 AC 79 ms
15,156 KB
testcase_04 AC 244 ms
21,872 KB
testcase_05 AC 399 ms
19,836 KB
testcase_06 AC 291 ms
18,564 KB
testcase_07 AC 172 ms
15,608 KB
testcase_08 AC 201 ms
20,088 KB
testcase_09 AC 424 ms
25,472 KB
testcase_10 AC 230 ms
17,240 KB
testcase_11 AC 510 ms
26,172 KB
testcase_12 AC 484 ms
22,648 KB
testcase_13 AC 159 ms
17,588 KB
testcase_14 AC 373 ms
25,544 KB
testcase_15 AC 512 ms
26,428 KB
testcase_16 AC 476 ms
26,848 KB
testcase_17 AC 194 ms
17,368 KB
testcase_18 AC 386 ms
20,044 KB
testcase_19 AC 396 ms
21,316 KB
testcase_20 AC 490 ms
22,524 KB
testcase_21 AC 321 ms
21,984 KB
testcase_22 AC 588 ms
29,056 KB
testcase_23 AC 196 ms
18,896 KB
testcase_24 AC 290 ms
21,952 KB
testcase_25 AC 394 ms
19,936 KB
testcase_26 AC 505 ms
24,968 KB
testcase_27 AC 605 ms
26,748 KB
testcase_28 AC 319 ms
18,976 KB
testcase_29 AC 417 ms
22,300 KB
testcase_30 AC 262 ms
22,412 KB
testcase_31 AC 290 ms
15,268 KB
testcase_32 AC 261 ms
19,860 KB
testcase_33 AC 375 ms
17,364 KB
testcase_34 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, Q = gets.split.map(&:to_i)
E = Hash.new { |h, k| h[k] = [] }

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

C = Array.new(N + 1, 0)

def dfs(v, visited)
  cnt = 1
  visited[v] = true

  E[v].each do |u|
    next if visited[u]

    cnt += dfs(u, visited)
  end

  C[v] = cnt
end

visited = Array.new(N + 1, false)
dfs(1, visited)
ans = 0

Q.times do
  p, x = gets.split.map(&:to_i)
  ans += C[p] * x

  puts ans
end
0