結果
問題 |
No.1207 グラフX
|
ユーザー |
|
提出日時 | 2020-08-30 18:41:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,892 ms / 2,000 ms |
コード長 | 1,185 bytes |
コンパイル時間 | 430 ms |
コンパイル使用メモリ | 82,632 KB |
実行使用メモリ | 343,160 KB |
最終ジャッジ日時 | 2025-04-01 15:58:22 |
合計ジャッジ時間 | 42,408 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * 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 if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def same(self, x, y): return self.find(x) == self.find(y) N, M, X = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(M)] uf = UnionFind(N) edge = [[] for _ in range(N)] num = 0 for x,y,z in A: x -= 1 y -= 1 if not uf.same(x,y): uf.union(x,y) edge[x].append((y,z,num)) edge[y].append((x,z,num)) num += 1 if num==N-1: break import sys sys.setrecursionlimit(10**6) def dfs(v): global ans res = 0 for u,z,num in edge[v]: if used[num]==False: used[num] = True v_num = dfs(u) ans += v_num*(N-v_num)*pow(X,z,mod) ans %= mod res += v_num return res+1 used = [False]*(N-1) ans = 0 mod = 10**9+7 dfs(0) print(ans)