結果

問題 No.1207 グラフX
ユーザー H3PO4H3PO4
提出日時 2021-12-25 16:03:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 911 ms / 2,000 ms
コード長 1,580 bytes
コンパイル時間 533 ms
コンパイル使用メモリ 80,592 KB
実行使用メモリ 159,208 KB
最終ジャッジ日時 2023-10-21 17:21:26
合計ジャッジ時間 31,079 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 674 ms
151,996 KB
testcase_01 AC 589 ms
152,732 KB
testcase_02 AC 590 ms
153,696 KB
testcase_03 AC 624 ms
159,208 KB
testcase_04 AC 576 ms
152,608 KB
testcase_05 AC 887 ms
143,332 KB
testcase_06 AC 911 ms
144,664 KB
testcase_07 AC 875 ms
143,668 KB
testcase_08 AC 510 ms
114,708 KB
testcase_09 AC 534 ms
131,832 KB
testcase_10 AC 885 ms
140,300 KB
testcase_11 AC 900 ms
143,760 KB
testcase_12 AC 527 ms
125,184 KB
testcase_13 AC 313 ms
85,676 KB
testcase_14 AC 612 ms
152,988 KB
testcase_15 AC 550 ms
126,236 KB
testcase_16 AC 306 ms
90,584 KB
testcase_17 AC 444 ms
111,196 KB
testcase_18 AC 356 ms
112,096 KB
testcase_19 AC 445 ms
103,920 KB
testcase_20 AC 602 ms
148,640 KB
testcase_21 AC 86 ms
76,660 KB
testcase_22 AC 461 ms
111,852 KB
testcase_23 AC 469 ms
118,248 KB
testcase_24 AC 328 ms
111,248 KB
testcase_25 AC 625 ms
152,972 KB
testcase_26 AC 528 ms
134,292 KB
testcase_27 AC 587 ms
136,760 KB
testcase_28 AC 570 ms
128,420 KB
testcase_29 AC 576 ms
147,140 KB
testcase_30 AC 327 ms
96,912 KB
testcase_31 AC 263 ms
84,536 KB
testcase_32 AC 298 ms
103,140 KB
testcase_33 AC 330 ms
98,340 KB
testcase_34 AC 546 ms
127,888 KB
testcase_35 AC 127 ms
77,688 KB
testcase_36 AC 575 ms
137,308 KB
testcase_37 AC 528 ms
125,844 KB
testcase_38 AC 215 ms
86,460 KB
testcase_39 AC 345 ms
103,776 KB
testcase_40 AC 174 ms
78,108 KB
testcase_41 AC 446 ms
99,856 KB
testcase_42 AC 39 ms
55,656 KB
testcase_43 AC 39 ms
55,656 KB
testcase_44 AC 39 ms
55,656 KB
testcase_45 AC 551 ms
157,676 KB
testcase_46 AC 603 ms
157,768 KB
testcase_47 AC 545 ms
157,768 KB
testcase_48 AC 541 ms
157,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.buffer.readline
MOD = 10 ** 9 + 7


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

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


N, M, X = map(int, input().split())
uf = UnionFind(N)
T = [[] for _ in range(N)]
for _ in range(M):
    x, y, z = map(int, input().split())
    x -= 1
    y -= 1
    if not uf.issame(x, y):
        uf.unite(x, y)
        T[x].append((y, z))
        T[y].append((x, z))

d = deque([(0, -1, 0)])
tour = []
while d:
    v, p, z = d.pop()
    tour.append((v, p, z))
    for x, xz in T[v]:
        if x != p:
            d.append((x, v, xz))

dp = [0] * N
ans = 0
for v, p, z in reversed(tour):
    dp[v] = 1
    for x, _ in T[v]:
        if x != p:
            dp[v] += dp[x]
    ans += pow(X, z, MOD) * dp[v] * (N - dp[v]) % MOD
ans %= MOD
print(ans)
0