結果
問題 | No.1207 グラフX |
ユーザー |
|
提出日時 | 2020-08-30 18:46:32 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,580 ms / 2,000 ms |
コード長 | 1,161 bytes |
コンパイル時間 | 1,760 ms |
コンパイル使用メモリ | 81,664 KB |
実行使用メモリ | 350,080 KB |
最終ジャッジ日時 | 2024-12-27 16:49:14 |
合計ジャッジ時間 | 38,219 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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) import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline 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 def dfs(v,p): global ans res = 0 for u,z,num in edge[v]: if u!=p: v_num = dfs(u,v) ans += v_num*(N-v_num)*pow(X,z,mod) ans %= mod res += v_num return res+1 ans = 0 mod = 10**9+7 dfs(0,-1) print(ans)