結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 488 ms
39,424 KB
testcase_01 AC 493 ms
39,424 KB
testcase_02 AC 476 ms
39,552 KB
testcase_03 AC 502 ms
39,552 KB
testcase_04 AC 501 ms
39,552 KB
testcase_05 AC 481 ms
37,504 KB
testcase_06 AC 487 ms
37,504 KB
testcase_07 AC 499 ms
37,504 KB
testcase_08 AC 383 ms
26,496 KB
testcase_09 AC 426 ms
34,816 KB
testcase_10 AC 518 ms
37,504 KB
testcase_11 AC 488 ms
37,504 KB
testcase_12 AC 378 ms
31,616 KB
testcase_13 AC 168 ms
13,568 KB
testcase_14 AC 480 ms
38,528 KB
testcase_15 AC 406 ms
33,280 KB
testcase_16 AC 185 ms
15,744 KB
testcase_17 AC 320 ms
25,344 KB
testcase_18 AC 266 ms
22,528 KB
testcase_19 AC 276 ms
20,864 KB
testcase_20 AC 493 ms
39,040 KB
testcase_21 AC 28 ms
6,940 KB
testcase_22 AC 327 ms
25,600 KB
testcase_23 AC 346 ms
31,488 KB
testcase_24 AC 226 ms
21,120 KB
testcase_25 AC 496 ms
38,912 KB
testcase_26 AC 382 ms
32,768 KB
testcase_27 AC 457 ms
36,608 KB
testcase_28 AC 430 ms
34,944 KB
testcase_29 AC 425 ms
36,480 KB
testcase_30 AC 204 ms
17,536 KB
testcase_31 AC 139 ms
12,416 KB
testcase_32 AC 177 ms
17,920 KB
testcase_33 AC 193 ms
17,664 KB
testcase_34 AC 404 ms
33,024 KB
testcase_35 AC 31 ms
6,944 KB
testcase_36 AC 409 ms
34,688 KB
testcase_37 AC 335 ms
26,368 KB
testcase_38 AC 84 ms
8,704 KB
testcase_39 AC 205 ms
18,944 KB
testcase_40 AC 101 ms
9,984 KB
testcase_41 AC 261 ms
21,120 KB
testcase_42 AC 1 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 1 ms
6,944 KB
testcase_45 AC 492 ms
39,424 KB
testcase_46 AC 465 ms
39,424 KB
testcase_47 AC 460 ms
39,424 KB
testcase_48 AC 477 ms
39,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mod = 1000000007
local mfl = math.floor

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 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