結果

問題 No.1637 Easy Tree Query
ユーザー 小野寺健小野寺健
提出日時 2021-11-01 16:59:35
言語 Ruby
(3.3.0)
結果
AC  
実行時間 624 ms / 2,000 ms
コード長 634 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 31,232 KB
最終ジャッジ日時 2024-04-18 14:17:23
合計ジャッジ時間 15,746 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
12,160 KB
testcase_01 AC 90 ms
12,160 KB
testcase_02 AC 624 ms
27,776 KB
testcase_03 AC 87 ms
12,160 KB
testcase_04 AC 238 ms
18,048 KB
testcase_05 AC 433 ms
21,760 KB
testcase_06 AC 320 ms
18,560 KB
testcase_07 AC 202 ms
15,104 KB
testcase_08 AC 211 ms
17,408 KB
testcase_09 AC 456 ms
24,704 KB
testcase_10 AC 240 ms
17,024 KB
testcase_11 AC 537 ms
25,216 KB
testcase_12 AC 525 ms
24,320 KB
testcase_13 AC 177 ms
14,848 KB
testcase_14 AC 366 ms
22,016 KB
testcase_15 AC 520 ms
26,112 KB
testcase_16 AC 466 ms
27,008 KB
testcase_17 AC 189 ms
16,000 KB
testcase_18 AC 407 ms
21,760 KB
testcase_19 AC 419 ms
21,760 KB
testcase_20 AC 505 ms
24,320 KB
testcase_21 AC 336 ms
20,480 KB
testcase_22 AC 609 ms
27,648 KB
testcase_23 AC 227 ms
16,768 KB
testcase_24 AC 289 ms
20,352 KB
testcase_25 AC 488 ms
21,760 KB
testcase_26 AC 525 ms
24,576 KB
testcase_27 AC 615 ms
31,232 KB
testcase_28 AC 350 ms
21,376 KB
testcase_29 AC 458 ms
23,808 KB
testcase_30 AC 277 ms
18,688 KB
testcase_31 AC 348 ms
19,072 KB
testcase_32 AC 268 ms
19,072 KB
testcase_33 AC 394 ms
22,912 KB
testcase_34 AC 314 ms
21,760 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, Q = gets.split(" ").map{|s| s.to_i}

v = Array.new(N) {Array.new}

(N-1).times {
	a, b = gets.split(" ").map{|s| s.to_i}
	v[b-1] << a-1
	v[a-1] << b-1
}

q = []

Q.times {
	q << gets.split(" ").map{|s| s.to_i}
}

stack = []
stack << [-1, 0]
sorted_nodes = []

until stack.empty?
  parent, node = stack.pop
  sorted_nodes << node
  v[node].each do |child|
    stack << [node, child] if child != parent
  end
end

counts = Array.new(N, 0)

sorted_nodes.reverse_each do |node|
  ret = 1
  v[node].each do |child|
    ret += counts[child]
  end
  counts[node] = ret
end

total = 0
q.each {|p, x|
	total += counts[p-1] * x
	puts total
}
0