結果

問題 No.1637 Easy Tree Query
ユーザー 👑 obakyanobakyan
提出日時 2021-08-06 21:28:16
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 1,026 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 5,336 KB
実行使用メモリ 33,948 KB
最終ジャッジ日時 2023-10-17 04:53:42
合計ジャッジ時間 7,002 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 257 ms
33,948 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 67 ms
12,088 KB
testcase_05 AC 179 ms
19,136 KB
testcase_06 AC 106 ms
13,408 KB
testcase_07 AC 44 ms
4,348 KB
testcase_08 AC 55 ms
10,384 KB
testcase_09 AC 153 ms
23,188 KB
testcase_10 AC 73 ms
11,752 KB
testcase_11 AC 196 ms
25,832 KB
testcase_12 AC 199 ms
25,360 KB
testcase_13 AC 34 ms
7,748 KB
testcase_14 AC 112 ms
18,920 KB
testcase_15 AC 197 ms
25,184 KB
testcase_16 AC 156 ms
21,952 KB
testcase_17 AC 50 ms
9,756 KB
testcase_18 AC 151 ms
19,160 KB
testcase_19 AC 163 ms
21,564 KB
testcase_20 AC 218 ms
25,424 KB
testcase_21 AC 100 ms
16,600 KB
testcase_22 AC 220 ms
29,992 KB
testcase_23 AC 50 ms
8,512 KB
testcase_24 AC 84 ms
14,524 KB
testcase_25 AC 158 ms
18,336 KB
testcase_26 AC 206 ms
29,388 KB
testcase_27 AC 243 ms
31,220 KB
testcase_28 AC 115 ms
14,780 KB
testcase_29 AC 157 ms
23,164 KB
testcase_30 AC 66 ms
12,328 KB
testcase_31 AC 93 ms
4,348 KB
testcase_32 AC 78 ms
14,744 KB
testcase_33 AC 137 ms
14,796 KB
testcase_34 AC 77 ms
19,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, q = io.read("*n", "*n")
local edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  table.insert(edge[a], b)
  table.insert(edge[b], a)
end
local tasks = {1}
local parent = {}
local weight = {}
local ccnt = {}
for i = 1, n do
  parent[i] = 0
  weight[i] = 1
  ccnt[i] = 0
end
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if parent[src] ~= dst then
      parent[dst] = src
      table.insert(tasks, dst)
      ccnt[src] = ccnt[src] + 1
    end
  end
end
tasks = {}
for i = 1, n do
  if ccnt[i] == 0 then
    table.insert(tasks, i)
  end
end
for i = 1, n do
  local src = tasks[i]
  local p = parent[src]
  if 0 < p then
    weight[p] = weight[p] + weight[src]
    ccnt[p] = ccnt[p] - 1
    if ccnt[p] == 0 then
      table.insert(tasks, p)
    end
  end
end
local ret = 0LL
for iq = 1, q do
  local z, x = io.read("*n", "*n")
  ret = ret + 1LL * x * weight[z]
  local a = tostring(ret):gsub("LL", "")
  print(a)
end
0