結果

問題 No.1207 グラフX
ユーザー lloyzlloyz
提出日時 2023-08-11 00:14:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,280 ms / 2,000 ms
コード長 2,608 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 197,184 KB
最終ジャッジ日時 2024-04-28 17:24:52
合計ジャッジ時間 40,481 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 709 ms
179,540 KB
testcase_01 AC 739 ms
179,576 KB
testcase_02 AC 731 ms
178,616 KB
testcase_03 AC 691 ms
181,380 KB
testcase_04 AC 700 ms
180,028 KB
testcase_05 AC 1,199 ms
161,280 KB
testcase_06 AC 1,211 ms
176,648 KB
testcase_07 AC 1,174 ms
161,976 KB
testcase_08 AC 703 ms
131,388 KB
testcase_09 AC 711 ms
166,056 KB
testcase_10 AC 1,201 ms
166,744 KB
testcase_11 AC 1,280 ms
162,384 KB
testcase_12 AC 675 ms
152,616 KB
testcase_13 AC 448 ms
91,776 KB
testcase_14 AC 769 ms
192,192 KB
testcase_15 AC 712 ms
165,888 KB
testcase_16 AC 546 ms
96,236 KB
testcase_17 AC 635 ms
131,292 KB
testcase_18 AC 467 ms
137,332 KB
testcase_19 AC 557 ms
108,764 KB
testcase_20 AC 710 ms
195,992 KB
testcase_21 AC 204 ms
77,056 KB
testcase_22 AC 768 ms
130,520 KB
testcase_23 AC 596 ms
138,592 KB
testcase_24 AC 421 ms
129,700 KB
testcase_25 AC 837 ms
195,408 KB
testcase_26 AC 653 ms
156,680 KB
testcase_27 AC 771 ms
176,468 KB
testcase_28 AC 808 ms
166,020 KB
testcase_29 AC 726 ms
177,144 KB
testcase_30 AC 466 ms
110,824 KB
testcase_31 AC 371 ms
86,144 KB
testcase_32 AC 429 ms
116,464 KB
testcase_33 AC 467 ms
110,144 KB
testcase_34 AC 764 ms
156,220 KB
testcase_35 AC 213 ms
78,592 KB
testcase_36 AC 831 ms
164,680 KB
testcase_37 AC 665 ms
143,480 KB
testcase_38 AC 300 ms
89,932 KB
testcase_39 AC 456 ms
116,376 KB
testcase_40 AC 279 ms
78,772 KB
testcase_41 AC 644 ms
110,216 KB
testcase_42 AC 50 ms
54,272 KB
testcase_43 AC 49 ms
53,888 KB
testcase_44 AC 50 ms
54,272 KB
testcase_45 AC 661 ms
196,572 KB
testcase_46 AC 669 ms
197,100 KB
testcase_47 AC 672 ms
197,092 KB
testcase_48 AC 664 ms
197,184 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())

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]) * pow(x, z, mod) % mod
        ans %= mod
        Size[cp] += Size[np]
print(ans)
0