結果

問題 No.1637 Easy Tree Query
ユーザー simansiman
提出日時 2021-11-26 15:50:21
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 791 bytes
コンパイル時間 61 ms
コンパイル使用メモリ 11,580 KB
実行使用メモリ 60,444 KB
最終ジャッジ日時 2023-09-11 22:56:52
合計ジャッジ時間 22,197 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,036 KB
testcase_01 AC 80 ms
15,144 KB
testcase_02 AC 1,004 ms
57,752 KB
testcase_03 AC 81 ms
15,292 KB
testcase_04 AC 473 ms
39,632 KB
testcase_05 AC 574 ms
33,420 KB
testcase_06 AC 405 ms
27,460 KB
testcase_07 AC 182 ms
15,868 KB
testcase_08 AC 388 ms
35,232 KB
testcase_09 AC 756 ms
52,956 KB
testcase_10 AC 326 ms
25,200 KB
testcase_11 AC 897 ms
56,180 KB
testcase_12 AC 756 ms
45,048 KB
testcase_13 AC 260 ms
25,864 KB
testcase_14 AC 699 ms
53,516 KB
testcase_15 AC 887 ms
58,136 KB
testcase_16 AC 893 ms
60,444 KB
testcase_17 AC 287 ms
25,940 KB
testcase_18 AC 543 ms
33,576 KB
testcase_19 AC 586 ms
36,860 KB
testcase_20 AC 761 ms
40,652 KB
testcase_21 AC 556 ms
40,456 KB
testcase_22 AC 1,014 ms
60,216 KB
testcase_23 AC 317 ms
28,052 KB
testcase_24 AC 523 ms
40,400 KB
testcase_25 AC 549 ms
31,096 KB
testcase_26 AC 801 ms
50,444 KB
testcase_27 AC 1,005 ms
59,744 KB
testcase_28 AC 448 ms
28,916 KB
testcase_29 AC 667 ms
40,840 KB
testcase_30 AC 531 ms
40,884 KB
testcase_31 AC 292 ms
15,336 KB
testcase_32 AC 424 ms
32,720 KB
testcase_33 AC 466 ms
25,628 KB
testcase_34 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'tsort'

class Hash
  include TSort
  alias tsort_each_node each_key
  def tsort_each_child(node, &block)
    fetch(node).each(&block)
  end
end

N, Q = gets.split.map(&:to_i)
OE = Array.new(N + 1) { [] }
E = Hash.new
RE = Hash.new { |h, k| h[k] = [] }
1.upto(N) do |v|
  E[v] = []
end

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

visited = Array.new(N + 1, false)
queue = [1]
until queue.empty?
  v = queue.shift

  next if visited[v]
  visited[v] = true

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

    E[v] << u
    RE[u] << v
    queue << u
  end
end

C = Array.new(N + 1, 0)
E.tsort.each do |v|
  C[v] += 1

  RE[v].each do |u|
    C[u] += C[v]
  end
end

ans = 0

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

  puts ans
end
0