結果

問題 No.1207 グラフX
ユーザー chineristACchineristAC
提出日時 2020-08-30 13:44:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,349 ms / 2,000 ms
コード長 1,685 bytes
コンパイル時間 445 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 311,488 KB
最終ジャッジ日時 2024-04-27 07:04:43
合計ジャッジ時間 27,550 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 543 ms
120,220 KB
testcase_01 AC 547 ms
120,496 KB
testcase_02 AC 533 ms
119,328 KB
testcase_03 AC 531 ms
119,112 KB
testcase_04 AC 535 ms
119,888 KB
testcase_05 AC 1,158 ms
299,492 KB
testcase_06 AC 1,202 ms
294,056 KB
testcase_07 AC 1,210 ms
302,244 KB
testcase_08 AC 517 ms
102,296 KB
testcase_09 AC 532 ms
109,944 KB
testcase_10 AC 1,349 ms
311,488 KB
testcase_11 AC 1,198 ms
298,932 KB
testcase_12 AC 519 ms
105,156 KB
testcase_13 AC 292 ms
83,836 KB
testcase_14 AC 556 ms
119,456 KB
testcase_15 AC 528 ms
108,616 KB
testcase_16 AC 307 ms
85,988 KB
testcase_17 AC 424 ms
100,404 KB
testcase_18 AC 350 ms
100,728 KB
testcase_19 AC 397 ms
91,332 KB
testcase_20 AC 568 ms
119,416 KB
testcase_21 AC 96 ms
77,008 KB
testcase_22 AC 448 ms
101,268 KB
testcase_23 AC 466 ms
105,308 KB
testcase_24 AC 332 ms
98,156 KB
testcase_25 AC 583 ms
118,608 KB
testcase_26 AC 483 ms
108,448 KB
testcase_27 AC 554 ms
113,740 KB
testcase_28 AC 548 ms
109,620 KB
testcase_29 AC 516 ms
113,112 KB
testcase_30 AC 330 ms
90,740 KB
testcase_31 AC 261 ms
82,412 KB
testcase_32 AC 279 ms
92,656 KB
testcase_33 AC 311 ms
91,628 KB
testcase_34 AC 527 ms
108,380 KB
testcase_35 AC 132 ms
78,724 KB
testcase_36 AC 522 ms
109,524 KB
testcase_37 AC 455 ms
102,128 KB
testcase_38 AC 212 ms
83,384 KB
testcase_39 AC 316 ms
93,168 KB
testcase_40 AC 177 ms
79,276 KB
testcase_41 AC 390 ms
91,768 KB
testcase_42 AC 41 ms
55,200 KB
testcase_43 AC 41 ms
54,176 KB
testcase_44 AC 40 ms
54,236 KB
testcase_45 AC 523 ms
118,972 KB
testcase_46 AC 512 ms
118,736 KB
testcase_47 AC 512 ms
118,860 KB
testcase_48 AC 516 ms
118,736 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
sys.setrecursionlimit(2*10**5)

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

size=[1 for i in range(N)]
def dfs(v,pv):
    for nv,c in edge[v]:
        if nv!=pv:
            dfs(nv,v)
            size[v]+=size[nv]

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

print(res)
0