結果
| 問題 | 
                            No.1637 Easy Tree Query
                             | 
                    
| コンテスト | |
| ユーザー | 
                            👑  | 
                    
| 提出日時 | 2021-08-06 21:28:16 | 
| 言語 | Lua  (LuaJit 2.1.1734355927)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 256 ms / 2,000 ms | 
| コード長 | 1,026 bytes | 
| コンパイル時間 | 291 ms | 
| コンパイル使用メモリ | 7,076 KB | 
| 実行使用メモリ | 33,844 KB | 
| 最終ジャッジ日時 | 2024-09-17 03:37:13 | 
| 合計ジャッジ時間 | 6,692 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
ソースコード
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