結果
問題 | No.1207 グラフX |
ユーザー | naniwazu |
提出日時 | 2020-08-30 17:56:17 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,093 bytes |
コンパイル時間 | 128 ms |
コンパイル使用メモリ | 82,168 KB |
実行使用メモリ | 155,684 KB |
最終ジャッジ日時 | 2024-11-15 14:44:23 |
合計ジャッジ時間 | 27,261 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 830 ms
136,052 KB |
testcase_06 | AC | 832 ms
136,704 KB |
testcase_07 | AC | 807 ms
136,052 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 862 ms
136,772 KB |
testcase_11 | AC | 817 ms
136,320 KB |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | AC | 36 ms
52,352 KB |
testcase_43 | AC | 36 ms
52,864 KB |
testcase_44 | AC | 38 ms
52,864 KB |
testcase_45 | AC | 586 ms
149,632 KB |
testcase_46 | AC | 539 ms
149,608 KB |
testcase_47 | AC | 532 ms
149,668 KB |
testcase_48 | AC | 523 ms
149,608 KB |
ソースコード
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 size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): return {r: self.members(r) for r in self.roots()} def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) import sys sys.setrecursionlimit(1<<30) mod = 10**9+7 N,M,X = map(int,input().split()) Tree = [[] for _ in range(N)] edges = [] uf = UnionFind(N) for i in range(M): x,y,z = map(int,input().split()) if not uf.same(x-1,y-1): uf.union(x-1,y-1) Tree[x-1].append(y-1) Tree[y-1].append(x-1) edges.append((x,y,z)) size = [1]*N parent = [-1]*N d = [] d.append(0) while d: x = d.pop() if x == 0: if size[x] == 1: for y in Tree[x]: if parent[x] != y: d.append(y) parent[y] = x elif size[x] > 1 or len(Tree[x]) == 1: p = parent[x] size[p] += size[x] d.append(p) else: for y in Tree[x]: if parent[x] != y: d.append(y) parent[y] = x ans = 0 for x,y,z in edges: s = min(size[x-1],size[y-1]) ans += s*(N-s)*pow(X,z,mod) ans %= mod print(ans)