結果

問題 No.1207 グラフX
ユーザー chineristACchineristAC
提出日時 2020-08-30 13:48:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 952 ms / 2,000 ms
コード長 1,952 bytes
コンパイル時間 484 ms
コンパイル使用メモリ 87,288 KB
実行使用メモリ 137,020 KB
最終ジャッジ日時 2023-08-09 12:02:03
合計ジャッジ時間 30,851 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 655 ms
135,956 KB
testcase_01 AC 650 ms
135,952 KB
testcase_02 AC 677 ms
136,252 KB
testcase_03 AC 630 ms
136,044 KB
testcase_04 AC 647 ms
136,368 KB
testcase_05 AC 929 ms
129,784 KB
testcase_06 AC 927 ms
130,224 KB
testcase_07 AC 952 ms
133,372 KB
testcase_08 AC 643 ms
114,040 KB
testcase_09 AC 628 ms
119,436 KB
testcase_10 AC 950 ms
131,712 KB
testcase_11 AC 930 ms
129,812 KB
testcase_12 AC 586 ms
115,848 KB
testcase_13 AC 361 ms
88,236 KB
testcase_14 AC 677 ms
134,084 KB
testcase_15 AC 645 ms
124,544 KB
testcase_16 AC 410 ms
90,548 KB
testcase_17 AC 532 ms
108,928 KB
testcase_18 AC 436 ms
110,260 KB
testcase_19 AC 478 ms
99,032 KB
testcase_20 AC 681 ms
132,216 KB
testcase_21 AC 145 ms
78,852 KB
testcase_22 AC 535 ms
108,996 KB
testcase_23 AC 538 ms
115,244 KB
testcase_24 AC 409 ms
107,612 KB
testcase_25 AC 672 ms
134,064 KB
testcase_26 AC 598 ms
121,496 KB
testcase_27 AC 654 ms
122,988 KB
testcase_28 AC 661 ms
120,824 KB
testcase_29 AC 628 ms
129,472 KB
testcase_30 AC 407 ms
98,356 KB
testcase_31 AC 326 ms
85,436 KB
testcase_32 AC 364 ms
99,244 KB
testcase_33 AC 405 ms
97,552 KB
testcase_34 AC 618 ms
118,000 KB
testcase_35 AC 191 ms
80,348 KB
testcase_36 AC 621 ms
122,920 KB
testcase_37 AC 556 ms
114,128 KB
testcase_38 AC 282 ms
86,640 KB
testcase_39 AC 390 ms
100,956 KB
testcase_40 AC 240 ms
80,548 KB
testcase_41 AC 466 ms
99,064 KB
testcase_42 AC 88 ms
71,692 KB
testcase_43 AC 88 ms
71,620 KB
testcase_44 AC 88 ms
71,624 KB
testcase_45 AC 597 ms
136,844 KB
testcase_46 AC 605 ms
137,020 KB
testcase_47 AC 590 ms
136,888 KB
testcase_48 AC 590 ms
136,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans

import sys
from collections import deque

input=sys.stdin.readline


N,M,X=map(int,input().split())
uf=UnionFindVerSize(N)
ans=0
mod=10**9+7

MST = []
edge = [[] for i in range(N)]
for i in range(M):
    x,y,z=map(int,input().split())
    x-=1
    y-=1
    if not uf.is_same_group(x,y):
        uf.unite(x,y)
        edge[x].append((y,z))
        edge[y].append((x,z))

deq=deque([0])
ans=[]
used=[False]*N
used[0]=True
while deq:
    v=deq.popleft()
    ans.append(v)
    for nv,c in edge[v]:
        if not used[nv]:
            used[nv]=True
            deq.append(nv)

size=[-1 for i in range(N)]

for v in ans[::-1]:
    size[v]=1
    for nv,c in edge[v]:
        if not size[nv]==-1:
            size[v]+=size[nv]

res=0
for u in range(N):
    for v,c in edge[u]:
        if size[u]<size[v]:
            tmp=pow(X,c,mod)
            tmp*=size[u]
            tmp%=mod
            tmp*=(N-size[u])
            tmp%=mod
            res+=tmp
            res%=mod

print(res)
0