結果

問題 No.1207 グラフX
ユーザー 👑 obakyanobakyan
提出日時 2021-08-12 15:20:26
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 454 ms / 2,000 ms
コード長 2,188 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 39,552 KB
最終ジャッジ日時 2024-04-09 19:53:53
合計ジャッジ時間 18,423 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 454 ms
39,424 KB
testcase_01 AC 450 ms
39,424 KB
testcase_02 AC 439 ms
39,552 KB
testcase_03 AC 453 ms
39,552 KB
testcase_04 AC 443 ms
39,552 KB
testcase_05 AC 432 ms
37,504 KB
testcase_06 AC 437 ms
37,504 KB
testcase_07 AC 442 ms
37,504 KB
testcase_08 AC 334 ms
26,496 KB
testcase_09 AC 375 ms
34,816 KB
testcase_10 AC 438 ms
37,504 KB
testcase_11 AC 447 ms
37,504 KB
testcase_12 AC 352 ms
31,616 KB
testcase_13 AC 155 ms
13,568 KB
testcase_14 AC 445 ms
38,528 KB
testcase_15 AC 364 ms
33,280 KB
testcase_16 AC 164 ms
15,744 KB
testcase_17 AC 280 ms
25,344 KB
testcase_18 AC 241 ms
22,528 KB
testcase_19 AC 255 ms
20,864 KB
testcase_20 AC 437 ms
39,040 KB
testcase_21 AC 26 ms
6,944 KB
testcase_22 AC 276 ms
25,600 KB
testcase_23 AC 302 ms
31,488 KB
testcase_24 AC 211 ms
21,120 KB
testcase_25 AC 445 ms
38,912 KB
testcase_26 AC 354 ms
32,768 KB
testcase_27 AC 404 ms
36,608 KB
testcase_28 AC 373 ms
35,072 KB
testcase_29 AC 375 ms
36,480 KB
testcase_30 AC 185 ms
17,408 KB
testcase_31 AC 128 ms
12,416 KB
testcase_32 AC 163 ms
17,920 KB
testcase_33 AC 177 ms
17,664 KB
testcase_34 AC 382 ms
33,024 KB
testcase_35 AC 29 ms
6,940 KB
testcase_36 AC 390 ms
34,688 KB
testcase_37 AC 316 ms
26,240 KB
testcase_38 AC 79 ms
8,704 KB
testcase_39 AC 239 ms
18,944 KB
testcase_40 AC 89 ms
9,856 KB
testcase_41 AC 235 ms
21,120 KB
testcase_42 AC 1 ms
6,944 KB
testcase_43 AC 1 ms
6,944 KB
testcase_44 AC 1 ms
6,940 KB
testcase_45 AC 432 ms
39,424 KB
testcase_46 AC 429 ms
39,424 KB
testcase_47 AC 418 ms
39,424 KB
testcase_48 AC 422 ms
39,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mod = 1000000007
local mfl = math.floor

local function bmul(x, y)
  if x * y <= 1000000000000000 then return (x * y) % mod end
  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 function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end

local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local n, m, x = io.read("*n", "*n", "*n")
local ea, eb, ec = {}, {}, {}
for i = 1, m do
  ea[i], eb[i], ec[i] = io.read("*n", "*n", "*n")
end
local edge = {}
for i = 1, n do
  edge[i] = {}
end

local parent = {}
for i = 1, n do
  parent[i] = i
end

local function uf_findroot(idx)
  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 used = 0
for i = 1, m do
  local i1, i2, c = ea[i], eb[i], ec[i]
  local r1, r2 = uf_findroot(i1, parent), uf_findroot(i2, parent)
  parent[i1], parent[i2] = r1, r2
  if r1 ~= r2 then
    parent[r2] = r1
    parent[i2] = r1
    used = used + 1
    table.insert(edge[i1], i)
    table.insert(edge[i2], i)
    if used == n - 1 then break end
  end
end

local size = {}
for i = 1, n do
  parent[i] = 0
  size[i] = 1
end
local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local ei = edge[src][j]
    local dst = ea[ei] == src and eb[ei] or ea[ei]
    if parent[src] ~= dst then
      parent[dst] = src
      table.insert(tasks, dst)
    end
  end
end
local ans = 0
for i = n, 1, -1 do
  local src = tasks[i]
  for j = 1, #edge[src] do
    local ei = edge[src][j]
    local dst = ea[ei] == src and eb[ei] or ea[ei]
    if parent[src] ~= dst then
      local csize = size[dst]
      ans = badd(ans, bmul(bmul(csize, n - csize), modpow(x, ec[ei])))
      size[src] = size[src] + csize
    end
  end
end
print(ans)
0