結果

問題 No.1207 グラフX
ユーザー brthyyjpbrthyyjp
提出日時 2020-09-06 22:38:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,584 ms / 2,000 ms
コード長 1,609 bytes
コンパイル時間 996 ms
コンパイル使用メモリ 85,556 KB
実行使用メモリ 241,536 KB
最終ジャッジ日時 2023-08-19 14:27:22
合計ジャッジ時間 46,573 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,195 ms
239,916 KB
testcase_01 AC 1,033 ms
239,944 KB
testcase_02 AC 1,033 ms
238,744 KB
testcase_03 AC 1,032 ms
241,536 KB
testcase_04 AC 1,029 ms
240,384 KB
testcase_05 AC 1,535 ms
225,080 KB
testcase_06 AC 1,510 ms
225,256 KB
testcase_07 AC 1,544 ms
224,620 KB
testcase_08 AC 863 ms
165,268 KB
testcase_09 AC 924 ms
198,540 KB
testcase_10 AC 1,521 ms
226,848 KB
testcase_11 AC 1,584 ms
225,800 KB
testcase_12 AC 860 ms
175,732 KB
testcase_13 AC 469 ms
102,372 KB
testcase_14 AC 1,047 ms
236,572 KB
testcase_15 AC 924 ms
187,096 KB
testcase_16 AC 471 ms
106,036 KB
testcase_17 AC 740 ms
157,140 KB
testcase_18 AC 634 ms
155,228 KB
testcase_19 AC 678 ms
128,020 KB
testcase_20 AC 1,066 ms
240,220 KB
testcase_21 AC 142 ms
78,812 KB
testcase_22 AC 756 ms
160,528 KB
testcase_23 AC 785 ms
174,692 KB
testcase_24 AC 582 ms
143,732 KB
testcase_25 AC 1,043 ms
234,396 KB
testcase_26 AC 861 ms
186,796 KB
testcase_27 AC 1,000 ms
210,884 KB
testcase_28 AC 924 ms
191,900 KB
testcase_29 AC 927 ms
208,532 KB
testcase_30 AC 578 ms
119,444 KB
testcase_31 AC 423 ms
94,676 KB
testcase_32 AC 477 ms
121,260 KB
testcase_33 AC 493 ms
121,640 KB
testcase_34 AC 842 ms
185,200 KB
testcase_35 AC 186 ms
81,052 KB
testcase_36 AC 842 ms
197,312 KB
testcase_37 AC 751 ms
169,508 KB
testcase_38 AC 319 ms
94,948 KB
testcase_39 AC 514 ms
128,264 KB
testcase_40 AC 250 ms
85,084 KB
testcase_41 AC 648 ms
131,116 KB
testcase_42 AC 75 ms
71,172 KB
testcase_43 AC 75 ms
71,288 KB
testcase_44 AC 74 ms
71,328 KB
testcase_45 AC 930 ms
240,920 KB
testcase_46 AC 912 ms
240,772 KB
testcase_47 AC 908 ms
241,144 KB
testcase_48 AC 918 ms
240,912 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