結果

問題 No.1207 グラフX
ユーザー lloyzlloyz
提出日時 2023-08-11 00:16:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,102 ms / 2,000 ms
コード長 2,614 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 82,604 KB
実行使用メモリ 197,372 KB
最終ジャッジ日時 2024-04-28 17:26:00
合計ジャッジ時間 33,395 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 641 ms
179,580 KB
testcase_01 AC 636 ms
179,820 KB
testcase_02 AC 614 ms
178,636 KB
testcase_03 AC 625 ms
181,252 KB
testcase_04 AC 633 ms
179,772 KB
testcase_05 AC 1,084 ms
161,292 KB
testcase_06 AC 1,102 ms
176,860 KB
testcase_07 AC 1,093 ms
162,060 KB
testcase_08 AC 652 ms
131,772 KB
testcase_09 AC 672 ms
165,944 KB
testcase_10 AC 1,082 ms
167,004 KB
testcase_11 AC 1,083 ms
162,524 KB
testcase_12 AC 637 ms
153,116 KB
testcase_13 AC 382 ms
91,932 KB
testcase_14 AC 690 ms
191,804 KB
testcase_15 AC 706 ms
165,872 KB
testcase_16 AC 416 ms
96,564 KB
testcase_17 AC 550 ms
131,288 KB
testcase_18 AC 440 ms
137,588 KB
testcase_19 AC 547 ms
110,292 KB
testcase_20 AC 689 ms
196,200 KB
testcase_21 AC 124 ms
77,432 KB
testcase_22 AC 554 ms
130,408 KB
testcase_23 AC 570 ms
138,748 KB
testcase_24 AC 396 ms
130,852 KB
testcase_25 AC 689 ms
195,188 KB
testcase_26 AC 608 ms
156,548 KB
testcase_27 AC 678 ms
176,844 KB
testcase_28 AC 685 ms
166,020 KB
testcase_29 AC 625 ms
177,464 KB
testcase_30 AC 427 ms
111,456 KB
testcase_31 AC 345 ms
86,144 KB
testcase_32 AC 363 ms
116,724 KB
testcase_33 AC 406 ms
110,400 KB
testcase_34 AC 664 ms
156,404 KB
testcase_35 AC 176 ms
79,120 KB
testcase_36 AC 642 ms
165,280 KB
testcase_37 AC 600 ms
143,980 KB
testcase_38 AC 266 ms
90,048 KB
testcase_39 AC 414 ms
116,504 KB
testcase_40 AC 254 ms
78,640 KB
testcase_41 AC 539 ms
109,956 KB
testcase_42 AC 42 ms
54,348 KB
testcase_43 AC 42 ms
54,308 KB
testcase_44 AC 43 ms
54,432 KB
testcase_45 AC 608 ms
197,084 KB
testcase_46 AC 605 ms
197,372 KB
testcase_47 AC 607 ms
197,372 KB
testcase_48 AC 603 ms
197,220 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]) % mod * pow(x, z, mod) % mod
        ans %= mod
        Size[cp] += Size[np]
print(ans)
0