結果

問題 No.1637 Easy Tree Query
ユーザー simansiman
提出日時 2021-11-26 15:39:40
言語 Ruby
(3.3.0)
結果
AC  
実行時間 715 ms / 2,000 ms
コード長 618 bytes
コンパイル時間 69 ms
コンパイル使用メモリ 11,476 KB
実行使用メモリ 147,028 KB
最終ジャッジ日時 2023-09-11 22:45:41
合計ジャッジ時間 18,618 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
15,256 KB
testcase_01 AC 161 ms
15,220 KB
testcase_02 AC 715 ms
26,308 KB
testcase_03 AC 159 ms
15,284 KB
testcase_04 AC 340 ms
21,848 KB
testcase_05 AC 496 ms
19,944 KB
testcase_06 AC 382 ms
18,860 KB
testcase_07 AC 257 ms
15,392 KB
testcase_08 AC 295 ms
19,952 KB
testcase_09 AC 536 ms
25,468 KB
testcase_10 AC 318 ms
17,180 KB
testcase_11 AC 642 ms
25,988 KB
testcase_12 AC 586 ms
22,844 KB
testcase_13 AC 244 ms
17,584 KB
testcase_14 AC 463 ms
25,676 KB
testcase_15 AC 626 ms
26,340 KB
testcase_16 AC 586 ms
28,832 KB
testcase_17 AC 275 ms
17,612 KB
testcase_18 AC 477 ms
19,832 KB
testcase_19 AC 484 ms
21,196 KB
testcase_20 AC 605 ms
22,616 KB
testcase_21 AC 409 ms
22,040 KB
testcase_22 AC 708 ms
29,044 KB
testcase_23 AC 294 ms
18,784 KB
testcase_24 AC 387 ms
21,796 KB
testcase_25 AC 497 ms
19,792 KB
testcase_26 AC 618 ms
25,036 KB
testcase_27 AC 715 ms
26,500 KB
testcase_28 AC 405 ms
19,044 KB
testcase_29 AC 518 ms
22,456 KB
testcase_30 AC 351 ms
22,456 KB
testcase_31 AC 378 ms
15,248 KB
testcase_32 AC 344 ms
20,012 KB
testcase_33 AC 455 ms
17,436 KB
testcase_34 AC 576 ms
147,028 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

if !ENV['RUBY_THREAD_VM_STACK_SIZE']
  exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'}, '/usr/bin/ruby', $0)
else
  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
end
0