結果

問題 No.1207 グラフX
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-03 23:36:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 710 ms / 2,000 ms
コード長 1,531 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 150,400 KB
最終ジャッジ日時 2024-04-21 18:29:01
合計ジャッジ時間 23,879 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 461 ms
147,584 KB
testcase_01 AC 449 ms
146,816 KB
testcase_02 AC 440 ms
144,256 KB
testcase_03 AC 460 ms
150,400 KB
testcase_04 AC 486 ms
149,120 KB
testcase_05 AC 692 ms
131,516 KB
testcase_06 AC 710 ms
131,336 KB
testcase_07 AC 697 ms
130,732 KB
testcase_08 AC 410 ms
114,432 KB
testcase_09 AC 442 ms
127,232 KB
testcase_10 AC 703 ms
131,240 KB
testcase_11 AC 703 ms
131,236 KB
testcase_12 AC 411 ms
120,704 KB
testcase_13 AC 255 ms
85,120 KB
testcase_14 AC 500 ms
138,752 KB
testcase_15 AC 433 ms
122,356 KB
testcase_16 AC 250 ms
88,064 KB
testcase_17 AC 358 ms
112,128 KB
testcase_18 AC 294 ms
114,108 KB
testcase_19 AC 345 ms
96,512 KB
testcase_20 AC 506 ms
145,152 KB
testcase_21 AC 89 ms
76,348 KB
testcase_22 AC 359 ms
113,280 KB
testcase_23 AC 381 ms
121,680 KB
testcase_24 AC 276 ms
109,336 KB
testcase_25 AC 507 ms
138,780 KB
testcase_26 AC 412 ms
125,824 KB
testcase_27 AC 476 ms
131,584 KB
testcase_28 AC 457 ms
127,872 KB
testcase_29 AC 436 ms
136,832 KB
testcase_30 AC 268 ms
95,232 KB
testcase_31 AC 216 ms
81,664 KB
testcase_32 AC 256 ms
98,432 KB
testcase_33 AC 251 ms
97,152 KB
testcase_34 AC 438 ms
122,240 KB
testcase_35 AC 111 ms
77,056 KB
testcase_36 AC 405 ms
129,412 KB
testcase_37 AC 366 ms
115,820 KB
testcase_38 AC 165 ms
83,328 KB
testcase_39 AC 262 ms
100,864 KB
testcase_40 AC 143 ms
77,440 KB
testcase_41 AC 342 ms
97,692 KB
testcase_42 AC 37 ms
52,352 KB
testcase_43 AC 35 ms
52,096 KB
testcase_44 AC 35 ms
51,968 KB
testcase_45 AC 424 ms
148,464 KB
testcase_46 AC 430 ms
148,224 KB
testcase_47 AC 430 ms
147,976 KB
testcase_48 AC 423 ms
148,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        self.root[x] = self.find(self.root[x])
        return self.root[x]

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

    def size(self, x):
        return -self.root[self.find(x)]


N, M, X = map(int, input().split())
uf = UF_tree(N)
G = [[] for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if uf.unite(a, b):
        G[a].append((b, c))
        G[b].append((a, c))

topo = []
par = [-1] * N
que = [0]
while que:
    s = que.pop()
    topo.append(s)
    for t, k in G[s]:
        if par[t] != -1:
            continue
        par[t] = (s, k)
        que.append(t)
topo.reverse()

ans = 0
size = [1] * N
for s in topo[:-1]:
    p, k = par[s]
    ans += size[s] * (N - size[s]) * pow(X, k, mod) % mod
    ans %= mod
    size[p] += size[s]

print(ans)
0