結果

問題 No.1207 グラフX
ユーザー lloyzlloyz
提出日時 2023-08-11 00:17:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,104 ms / 2,000 ms
コード長 2,653 bytes
コンパイル時間 578 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 190,236 KB
最終ジャッジ日時 2024-04-28 17:27:27
合計ジャッジ時間 29,662 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 603 ms
189,048 KB
testcase_01 AC 612 ms
188,668 KB
testcase_02 AC 595 ms
189,956 KB
testcase_03 AC 606 ms
190,236 KB
testcase_04 AC 600 ms
188,532 KB
testcase_05 AC 1,097 ms
175,440 KB
testcase_06 AC 1,100 ms
175,052 KB
testcase_07 AC 1,104 ms
175,868 KB
testcase_08 AC 613 ms
140,544 KB
testcase_09 AC 598 ms
149,992 KB
testcase_10 AC 1,053 ms
175,912 KB
testcase_11 AC 1,042 ms
175,636 KB
testcase_12 AC 584 ms
141,040 KB
testcase_13 AC 349 ms
90,868 KB
testcase_14 AC 615 ms
171,620 KB
testcase_15 AC 617 ms
159,572 KB
testcase_16 AC 368 ms
95,488 KB
testcase_17 AC 507 ms
133,572 KB
testcase_18 AC 402 ms
135,844 KB
testcase_19 AC 489 ms
110,508 KB
testcase_20 AC 611 ms
174,040 KB
testcase_21 AC 114 ms
76,800 KB
testcase_22 AC 507 ms
128,552 KB
testcase_23 AC 528 ms
138,944 KB
testcase_24 AC 368 ms
126,548 KB
testcase_25 AC 623 ms
174,048 KB
testcase_26 AC 549 ms
152,276 KB
testcase_27 AC 621 ms
159,468 KB
testcase_28 AC 630 ms
162,720 KB
testcase_29 AC 589 ms
172,972 KB
testcase_30 AC 401 ms
109,152 KB
testcase_31 AC 320 ms
85,120 KB
testcase_32 AC 355 ms
113,376 KB
testcase_33 AC 381 ms
111,116 KB
testcase_34 AC 596 ms
144,444 KB
testcase_35 AC 160 ms
77,440 KB
testcase_36 AC 580 ms
161,400 KB
testcase_37 AC 541 ms
141,728 KB
testcase_38 AC 258 ms
87,936 KB
testcase_39 AC 368 ms
115,296 KB
testcase_40 AC 215 ms
78,404 KB
testcase_41 AC 481 ms
106,412 KB
testcase_42 AC 48 ms
54,016 KB
testcase_43 AC 49 ms
53,888 KB
testcase_44 AC 48 ms
53,760 KB
testcase_45 AC 555 ms
188,424 KB
testcase_46 AC 555 ms
188,572 KB
testcase_47 AC 552 ms
188,432 KB
testcase_48 AC 542 ms
188,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素xを含む集合の親を見つける関数。
        '''
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        要素xを含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素yを含む集合に統合される。
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        '''
        要素xを含む集合の要素数を出す関数。
        '''
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

import sys
input = sys.stdin.readline

n, m, x = map(int, input().split())
mod = 10**9 + 7
UF = UnionFind(n)
G = defaultdict(list)
for _ in range(m):
    u, v, z = map(int, input().split())
    u -= 1
    v -= 1
    if UF.same(u, v):
        continue
    UF.union(u, v)
    G[u].append((v, z))
    G[v].append((u, z))

Parents = [-1 for _ in range(n)]
visited = set([0])
Stack = [0]
Topo = []
while Stack:
    cp = Stack.pop()
    Topo.append(cp)
    for np, _ in G[cp]:
        if np in visited:
            continue
        visited.add(np)
        Parents[np] = cp
        Stack.append(np)

ans = 0
Size = [1 for _ in range(n)]
for cp in Topo[::-1]:
    for np, z in G[cp]:
        if np == Parents[cp]:
            continue
        ans += Size[np] * (n - Size[np]) % mod * pow(x, z, mod) % mod
        ans %= mod
        Size[cp] += Size[np]
print(ans)
0