結果

問題 No.1207 グラフX
ユーザー WSKRWSKR
提出日時 2020-09-22 22:47:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,468 ms / 2,000 ms
コード長 1,609 bytes
コンパイル時間 945 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 241,700 KB
最終ジャッジ日時 2023-09-09 05:35:00
合計ジャッジ時間 41,343 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 945 ms
240,064 KB
testcase_01 AC 950 ms
239,884 KB
testcase_02 AC 939 ms
239,252 KB
testcase_03 AC 967 ms
241,700 KB
testcase_04 AC 948 ms
240,608 KB
testcase_05 AC 1,402 ms
225,128 KB
testcase_06 AC 1,425 ms
225,212 KB
testcase_07 AC 1,440 ms
224,632 KB
testcase_08 AC 804 ms
165,464 KB
testcase_09 AC 848 ms
198,632 KB
testcase_10 AC 1,424 ms
226,844 KB
testcase_11 AC 1,468 ms
225,780 KB
testcase_12 AC 791 ms
175,884 KB
testcase_13 AC 448 ms
102,800 KB
testcase_14 AC 980 ms
236,596 KB
testcase_15 AC 863 ms
187,532 KB
testcase_16 AC 451 ms
106,004 KB
testcase_17 AC 696 ms
157,348 KB
testcase_18 AC 597 ms
155,376 KB
testcase_19 AC 643 ms
127,672 KB
testcase_20 AC 980 ms
240,372 KB
testcase_21 AC 130 ms
79,080 KB
testcase_22 AC 732 ms
160,708 KB
testcase_23 AC 751 ms
174,740 KB
testcase_24 AC 547 ms
143,716 KB
testcase_25 AC 967 ms
234,524 KB
testcase_26 AC 786 ms
186,980 KB
testcase_27 AC 896 ms
210,956 KB
testcase_28 AC 849 ms
191,796 KB
testcase_29 AC 861 ms
208,620 KB
testcase_30 AC 497 ms
119,388 KB
testcase_31 AC 379 ms
94,564 KB
testcase_32 AC 452 ms
121,388 KB
testcase_33 AC 490 ms
121,704 KB
testcase_34 AC 822 ms
185,468 KB
testcase_35 AC 181 ms
81,092 KB
testcase_36 AC 829 ms
197,388 KB
testcase_37 AC 732 ms
169,796 KB
testcase_38 AC 311 ms
95,232 KB
testcase_39 AC 497 ms
128,312 KB
testcase_40 AC 236 ms
85,376 KB
testcase_41 AC 615 ms
131,096 KB
testcase_42 AC 70 ms
71,524 KB
testcase_43 AC 71 ms
71,300 KB
testcase_44 AC 70 ms
71,268 KB
testcase_45 AC 899 ms
240,812 KB
testcase_46 AC 877 ms
241,072 KB
testcase_47 AC 879 ms
241,360 KB
testcase_48 AC 900 ms
240,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Find(x, par):
    if par[x] < 0:
        return x
    else:
        par[x] = Find(par[x], par)
        return par[x]

def Unite(x, y, par, rank):
    x = Find(x, par)
    y = Find(y, par)

    if x != y:
        if rank[x] < rank[y]:
            par[y] += par[x]
            par[x] = y
        else:
            par[x] += par[y]
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

def Same(x, y, par):
    return Find(x, par) == Find(y, par)

def Size(x, par):
    return -par[Find(x, par)]

def power(a, n, mod):
    bi=str(format(n,"b")) #2進数
    res=1
    for i in range(len(bi)):
        res=(res*res) %mod
        if bi[i]=="1":
            res=(res*a) %mod
    return res

import sys
import io, os
#input = sys.stdin.buffer.readline
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n, m, X = map(int, input().split())

par = [-1]* n
rank = [0]*n

ans = 0
mod = 10**9+7
g = [[] for _ in range(n)]
d = {}
for i in range(m):
    x, y, z = map(int, input().split())
    x, y = x-1, y-1
    if Same(x, y, par):
        continue
    else:
        Unite(x, y, par, rank)
        w = power(X, z, mod)
        d[(x, y)] = w
        d[(y, x)] = w
        g[x].append(y)
        g[y].append(x)

s = []
visit = [-1]*n
s.append(0)
order = []
par = [-1]*n
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if u == par[v]:
            continue
        par[u] = v
        s.append(u)

ans = 0
c = [1]*n
order.reverse()
for v in order:
    if par[v] != -1:
        ans += (n-c[v])*c[v]*d[(v, par[v])]
        c[par[v]] += c[v]
print(ans%mod)
0