結果

問題 No.1843 Tree ANDistance
ユーザー 👑 obakyanobakyan
提出日時 2022-04-26 23:55:03
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 461 ms / 2,000 ms
コード長 1,345 bytes
コンパイル時間 45 ms
コンパイル使用メモリ 5,212 KB
実行使用メモリ 45,540 KB
最終ジャッジ日時 2023-09-10 16:28:11
合計ジャッジ時間 11,762 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 378 ms
43,440 KB
testcase_01 AC 384 ms
42,520 KB
testcase_02 AC 349 ms
44,640 KB
testcase_03 AC 344 ms
43,396 KB
testcase_04 AC 386 ms
43,412 KB
testcase_05 AC 417 ms
30,992 KB
testcase_06 AC 461 ms
40,412 KB
testcase_07 AC 375 ms
42,452 KB
testcase_08 AC 372 ms
45,540 KB
testcase_09 AC 360 ms
39,340 KB
testcase_10 AC 329 ms
40,520 KB
testcase_11 AC 391 ms
42,396 KB
testcase_12 AC 409 ms
34,204 KB
testcase_13 AC 450 ms
39,320 KB
testcase_14 AC 15 ms
4,376 KB
testcase_15 AC 15 ms
4,380 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 7 ms
4,376 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 20 ms
4,528 KB
testcase_20 AC 12 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 213 ms
42,464 KB
testcase_29 AC 132 ms
19,464 KB
testcase_30 AC 134 ms
21,592 KB
testcase_31 AC 215 ms
41,208 KB
testcase_32 AC 213 ms
41,316 KB
testcase_33 AC 70 ms
8,760 KB
testcase_34 AC 140 ms
21,544 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mod = 1000000007

local function bmul(x, y)
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  local x0, y0 = x - x1 * 31623, y - y1 * 31623
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local n = io.read("*n")
local a, b, c = {}, {}, {}
for i = 1, n - 1 do
  a[i], b[i], c[i] = io.read("*n", "*n", "*n")
end

local function uf_initialize(n)
  local parent = {}
  for i = 1, n do parent[i] = i end
  return parent
end

local function uf_findroot(idx, parent)
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end

local ans = 0
local mul = 1
for irep = 1, 32 do
  local parent = uf_initialize(n)
  for i = 1, n - 1 do
    local ai, bi, ci = a[i], b[i], c[i]
    c[i] = mfl(c[i] / 2)
    if ci % 2 == 1 then
      local ra, rb = uf_findroot(ai, parent), uf_findroot(bi, parent)
      parent[rb], parent[bi] = ra, ra
    end
  end
  local g = {}
  for i = 1, n do
    local r = uf_findroot(i, parent)
    if g[r] then g[r] = g[r] + 1 else g[r] = 1 end
  end
  local ret = 0
  for _k, sz in pairs(g) do
    ret = ret + mfl(sz * (sz - 1) / 2)
  end
  ans = ans + bmul(ret, mul)
  mul = (mul * 2) % mod
end
print(ans % mod)
0