結果

問題 No.1207 グラフX
ユーザー 👑 rin204rin204
提出日時 2022-10-30 17:14:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 956 ms / 2,000 ms
コード長 2,098 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 152,852 KB
最終ジャッジ日時 2023-09-21 14:54:23
合計ジャッジ時間 36,095 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 664 ms
145,952 KB
testcase_01 AC 659 ms
146,180 KB
testcase_02 AC 636 ms
144,732 KB
testcase_03 AC 631 ms
142,352 KB
testcase_04 AC 647 ms
146,176 KB
testcase_05 AC 938 ms
134,656 KB
testcase_06 AC 941 ms
134,236 KB
testcase_07 AC 956 ms
135,536 KB
testcase_08 AC 612 ms
114,900 KB
testcase_09 AC 628 ms
133,476 KB
testcase_10 AC 946 ms
136,452 KB
testcase_11 AC 948 ms
134,628 KB
testcase_12 AC 588 ms
124,056 KB
testcase_13 AC 384 ms
88,532 KB
testcase_14 AC 667 ms
148,996 KB
testcase_15 AC 672 ms
130,756 KB
testcase_16 AC 395 ms
91,212 KB
testcase_17 AC 531 ms
112,012 KB
testcase_18 AC 436 ms
118,660 KB
testcase_19 AC 498 ms
95,728 KB
testcase_20 AC 668 ms
152,800 KB
testcase_21 AC 140 ms
79,128 KB
testcase_22 AC 524 ms
112,820 KB
testcase_23 AC 548 ms
117,420 KB
testcase_24 AC 396 ms
112,008 KB
testcase_25 AC 662 ms
151,800 KB
testcase_26 AC 570 ms
129,932 KB
testcase_27 AC 645 ms
141,028 KB
testcase_28 AC 632 ms
134,856 KB
testcase_29 AC 605 ms
141,056 KB
testcase_30 AC 404 ms
98,532 KB
testcase_31 AC 343 ms
84,628 KB
testcase_32 AC 363 ms
101,696 KB
testcase_33 AC 383 ms
98,940 KB
testcase_34 AC 620 ms
129,576 KB
testcase_35 AC 191 ms
80,620 KB
testcase_36 AC 606 ms
132,728 KB
testcase_37 AC 546 ms
119,020 KB
testcase_38 AC 268 ms
86,916 KB
testcase_39 AC 388 ms
101,972 KB
testcase_40 AC 264 ms
80,216 KB
testcase_41 AC 474 ms
96,968 KB
testcase_42 AC 69 ms
71,248 KB
testcase_43 AC 69 ms
71,548 KB
testcase_44 AC 71 ms
71,312 KB
testcase_45 AC 585 ms
152,632 KB
testcase_46 AC 576 ms
152,852 KB
testcase_47 AC 581 ms
152,712 KB
testcase_48 AC 581 ms
152,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

    def find(self, 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 = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        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):
        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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


n, m, X = map(int, input().split())
UF = UnionFind(n)
edges = [[] for _ in range(n)]
for _ in range(m):
    x, y, z = map(int, input().split())
    x -= 1
    y -= 1
    if not UF.same(x, y):
        UF.union(x, y)
        edges[x].append((y, z))
        edges[y].append((x, z))

ans = 0
route = [0]
stack = [0]
used = [False] * n
used[0] = True
while stack:
    pos = stack.pop()
    for npos, _ in edges[pos]:
        if used[npos]:
            continue
        used[npos] = True
        stack.append(npos)
        route.append(npos)

size = [1] * n
used = [False] * n
for pos in route[::-1]:
    used[pos] = True
    for npos, z in edges[pos]:
        if not used[npos]:
            continue
        ans += pow(X, z, MOD) * size[npos] * (n - size[npos])
        ans %= MOD
        size[pos] += size[npos]
print(ans)
0