結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 👑 obakyanobakyan
提出日時 2021-07-22 00:21:45
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 1,099 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 5,336 KB
実行使用メモリ 17,324 KB
最終ジャッジ日時 2023-09-24 13:38:48
合計ジャッジ時間 5,816 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 152 ms
17,240 KB
testcase_04 AC 138 ms
17,060 KB
testcase_05 AC 140 ms
17,204 KB
testcase_06 AC 141 ms
17,252 KB
testcase_07 AC 142 ms
17,324 KB
testcase_08 AC 92 ms
14,824 KB
testcase_09 AC 41 ms
6,572 KB
testcase_10 AC 46 ms
8,692 KB
testcase_11 AC 25 ms
5,416 KB
testcase_12 AC 68 ms
10,000 KB
testcase_13 AC 80 ms
10,740 KB
testcase_14 AC 87 ms
14,512 KB
testcase_15 AC 63 ms
9,648 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 AC 9 ms
4,380 KB
testcase_18 AC 136 ms
16,496 KB
testcase_19 AC 24 ms
5,528 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 62 ms
9,624 KB
testcase_22 AC 55 ms
9,256 KB
testcase_23 AC 7 ms
4,376 KB
testcase_24 AC 6 ms
4,376 KB
testcase_25 AC 5 ms
4,380 KB
testcase_26 AC 6 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 8 ms
4,380 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 5 ms
4,380 KB
testcase_33 AC 24 ms
5,736 KB
testcase_34 WA -
testcase_35 AC 43 ms
8,844 KB
testcase_36 AC 13 ms
4,376 KB
testcase_37 AC 104 ms
17,000 KB
testcase_38 AC 93 ms
16,516 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n = io.read("*n")
local edge = {}
local parent = {}
local childcnt = {}
local w = {}
local asked = {}
for i = 1, n do
  edge[i] = {}
  parent[i] = 0
  childcnt[i] = 0
  w[i] = 1
  asked[i] = false
end
for i = 1, n - 1 do
  local p, q = io.read("*n", "*n")
  table.insert(edge[p], q)
  table.insert(edge[q], p)
end
local tasks = {1}
asked[1] = true
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if not asked[dst] then
      asked[dst] = true
      childcnt[src] = childcnt[src] + 1
      table.insert(tasks, dst)
      parent[dst] = src
    end
  end
end

local ret = n * n
tasks = {}
for i = 1, n do
  if childcnt[i] == 0 then table.insert(tasks, i) end
end
for i = 1, n do
  local src = tasks[i]
  ret = ret + (n - w[src]) * w[src]
  local p = parent[src]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if dst ~= p then
      ret = ret + w[dst] * (n - w[dst])
    end
  end
  if 0 < p then
    childcnt[p] = childcnt[p] - 1
    if childcnt[p] == 0 then table.insert(tasks, p) end
    w[p] = w[p] + w[src]
  end
end
print(ret)
0