結果
問題 | No.1207 グラフX |
ユーザー | 👑 obakyan |
提出日時 | 2021-08-12 15:20:26 |
言語 | Lua (LuaJit 2.1.1696795921) |
結果 |
AC
|
実行時間 | 583 ms / 2,000 ms |
コード長 | 2,188 bytes |
コンパイル時間 | 353 ms |
コンパイル使用メモリ | 6,948 KB |
実行使用メモリ | 39,680 KB |
最終ジャッジ日時 | 2024-10-01 20:07:50 |
合計ジャッジ時間 | 22,151 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 542 ms
39,424 KB |
testcase_01 | AC | 526 ms
39,424 KB |
testcase_02 | AC | 536 ms
39,680 KB |
testcase_03 | AC | 568 ms
39,424 KB |
testcase_04 | AC | 559 ms
39,552 KB |
testcase_05 | AC | 574 ms
37,504 KB |
testcase_06 | AC | 564 ms
37,632 KB |
testcase_07 | AC | 579 ms
37,504 KB |
testcase_08 | AC | 434 ms
26,496 KB |
testcase_09 | AC | 477 ms
34,944 KB |
testcase_10 | AC | 583 ms
37,632 KB |
testcase_11 | AC | 576 ms
37,504 KB |
testcase_12 | AC | 445 ms
31,616 KB |
testcase_13 | AC | 185 ms
13,696 KB |
testcase_14 | AC | 529 ms
38,528 KB |
testcase_15 | AC | 461 ms
33,280 KB |
testcase_16 | AC | 199 ms
15,744 KB |
testcase_17 | AC | 353 ms
25,472 KB |
testcase_18 | AC | 300 ms
22,528 KB |
testcase_19 | AC | 316 ms
20,864 KB |
testcase_20 | AC | 545 ms
39,040 KB |
testcase_21 | AC | 28 ms
6,816 KB |
testcase_22 | AC | 351 ms
25,600 KB |
testcase_23 | AC | 387 ms
31,488 KB |
testcase_24 | AC | 260 ms
21,248 KB |
testcase_25 | AC | 543 ms
38,912 KB |
testcase_26 | AC | 423 ms
32,768 KB |
testcase_27 | AC | 509 ms
36,608 KB |
testcase_28 | AC | 489 ms
34,944 KB |
testcase_29 | AC | 491 ms
36,480 KB |
testcase_30 | AC | 228 ms
17,408 KB |
testcase_31 | AC | 163 ms
12,544 KB |
testcase_32 | AC | 208 ms
17,920 KB |
testcase_33 | AC | 219 ms
17,664 KB |
testcase_34 | AC | 477 ms
33,024 KB |
testcase_35 | AC | 33 ms
6,816 KB |
testcase_36 | AC | 456 ms
34,688 KB |
testcase_37 | AC | 379 ms
26,240 KB |
testcase_38 | AC | 95 ms
8,704 KB |
testcase_39 | AC | 224 ms
18,944 KB |
testcase_40 | AC | 99 ms
9,856 KB |
testcase_41 | AC | 297 ms
21,120 KB |
testcase_42 | AC | 1 ms
6,816 KB |
testcase_43 | AC | 1 ms
6,820 KB |
testcase_44 | AC | 2 ms
6,816 KB |
testcase_45 | AC | 533 ms
39,424 KB |
testcase_46 | AC | 526 ms
39,424 KB |
testcase_47 | AC | 518 ms
39,424 KB |
testcase_48 | AC | 528 ms
39,552 KB |
ソースコード
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)