結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 160 ms
15,408 KB
testcase_01 AC 160 ms
15,360 KB
testcase_02 AC 713 ms
26,204 KB
testcase_03 AC 159 ms
15,392 KB
testcase_04 AC 343 ms
21,716 KB
testcase_05 AC 504 ms
19,800 KB
testcase_06 AC 386 ms
18,744 KB
testcase_07 AC 257 ms
15,640 KB
testcase_08 AC 300 ms
19,860 KB
testcase_09 AC 533 ms
25,500 KB
testcase_10 AC 317 ms
17,504 KB
testcase_11 AC 678 ms
26,004 KB
testcase_12 AC 576 ms
22,652 KB
testcase_13 AC 245 ms
17,656 KB
testcase_14 AC 461 ms
25,464 KB
testcase_15 AC 622 ms
26,388 KB
testcase_16 AC 590 ms
27,032 KB
testcase_17 AC 279 ms
17,536 KB
testcase_18 AC 478 ms
19,916 KB
testcase_19 AC 490 ms
21,280 KB
testcase_20 AC 594 ms
22,532 KB
testcase_21 AC 409 ms
21,876 KB
testcase_22 AC 681 ms
28,968 KB
testcase_23 AC 281 ms
18,712 KB
testcase_24 AC 383 ms
21,796 KB
testcase_25 AC 491 ms
19,984 KB
testcase_26 AC 619 ms
25,112 KB
testcase_27 AC 738 ms
26,784 KB
testcase_28 AC 411 ms
18,944 KB
testcase_29 AC 529 ms
22,280 KB
testcase_30 AC 363 ms
22,352 KB
testcase_31 AC 378 ms
15,444 KB
testcase_32 AC 348 ms
19,812 KB
testcase_33 AC 458 ms
17,244 KB
testcase_34 AC 550 ms
146,768 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

if !ENV['RUBY_THREAD_VM_STACK_SIZE']
  exec({'RUBY_THREAD_VM_STACK_SIZE'=>'1000000000'}, '/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