結果
| 問題 |
No.1207 グラフX
|
| コンテスト | |
| ユーザー |
tktk_snsn
|
| 提出日時 | 2021-01-03 23:36:09 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 905 ms / 2,000 ms |
| コード長 | 1,531 bytes |
| コンパイル時間 | 314 ms |
| コンパイル使用メモリ | 81,816 KB |
| 実行使用メモリ | 150,528 KB |
| 最終ジャッジ日時 | 2024-10-13 17:24:08 |
| 合計ジャッジ時間 | 28,175 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
ソースコード
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)
tktk_snsn