結果

問題 No.1098 LCAs
ユーザー 👑 obakyanobakyan
提出日時 2021-08-09 13:49:14
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 458 ms / 2,000 ms
コード長 1,249 bytes
コンパイル時間 435 ms
コンパイル使用メモリ 5,152 KB
実行使用メモリ 40,392 KB
最終ジャッジ日時 2023-10-21 11:32:27
合計ジャッジ時間 7,371 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 453 ms
39,672 KB
testcase_19 AC 439 ms
39,672 KB
testcase_20 AC 437 ms
39,644 KB
testcase_21 AC 440 ms
39,680 KB
testcase_22 AC 446 ms
39,656 KB
testcase_23 AC 345 ms
40,392 KB
testcase_24 AC 347 ms
40,392 KB
testcase_25 AC 305 ms
40,320 KB
testcase_26 AC 317 ms
40,320 KB
testcase_27 AC 308 ms
40,320 KB
testcase_28 AC 456 ms
39,840 KB
testcase_29 AC 458 ms
39,840 KB
testcase_30 AC 446 ms
39,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local edge = {}
local size = {}
local ccnt, parent = {}, {}
for i = 1, n do
  edge[i] = {}
  size[i] = 1
  ccnt[i] = 0
  parent[i] = 0
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}
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
      ccnt[src] = ccnt[src] + 1
      table.insert(tasks, dst)
    end
  end
end
local ans = {}
for i = 1, n do
  ans[i] = 0
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 v = 0
  local c = 0
  local p = parent[src]
  local sz = size[src]
  for j = 1, #edge[src] do
    local dst = edge[src][j]
    if dst == p then
      size[p] = size[p] + sz
      ccnt[p] = ccnt[p] - 1
      if ccnt[p] == 0 then
        table.insert(tasks, p)
      end
    else
      c = c + size[dst]
      v = v - mfl(size[dst] * (size[dst] - 1) / 2)
    end
  end
  v = v + mfl(c * (c - 1) / 2)
  ans[src] = v + sz - 1
end
for i = 1, n do
  ans[i] = ans[i] * 2 + 1
end
print(table.concat(ans, "\n"))
0