結果
問題 | No.1207 グラフX |
ユーザー |
![]() |
提出日時 | 2020-08-30 14:53:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,251 ms / 2,000 ms |
コード長 | 1,481 bytes |
コンパイル時間 | 1,493 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 138,200 KB |
最終ジャッジ日時 | 2024-12-27 15:31:33 |
合計ジャッジ時間 | 37,846 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
MOD = 10 ** 9 + 7 class UnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n+1) # 検索 def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] # 併合 def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.rank[x] < self.rank[y]: self.par[x] = y else: self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 # 同じ集合に属するか判定 def same_check(self, x, y): return self.find(x) == self.find(y) n, m, x = map(int, input().split()) uf = UnionFind(n) g = [[] for _ in range(n)] ans = 0 for _ in range(m): a, b, c = map(int, input().split()) if not uf.same_check(a - 1, b - 1): uf.unite(a - 1, b - 1) g[a - 1].append((b - 1, pow(x, c, MOD))) g[b - 1].append((a - 1, pow(x, c, MOD))) d = [-2] * n c = [1] * n s = [0] ans = 0 while s: p = s.pop() if d[p] == -2: s.append(p) d[p] = -1 for node, cost in g[p]: if d[node] == -2: s.append(node) else: d[p] = 0 for node, cost in g[p]: if d[node] >= 0: c[p] += c[node] ans += (n - c[node]) * c[node] * cost print(ans % MOD)