結果
問題 | No.1207 グラフX |
ユーザー | 👑 rin204 |
提出日時 | 2022-10-30 17:14:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 760 ms / 2,000 ms |
コード長 | 2,098 bytes |
コンパイル時間 | 196 ms |
コンパイル使用メモリ | 82,232 KB |
実行使用メモリ | 150,912 KB |
最終ジャッジ日時 | 2024-07-07 08:40:34 |
合計ジャッジ時間 | 25,225 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 496 ms
145,748 KB |
testcase_01 | AC | 486 ms
145,612 KB |
testcase_02 | AC | 471 ms
143,900 KB |
testcase_03 | AC | 480 ms
147,128 KB |
testcase_04 | AC | 485 ms
144,816 KB |
testcase_05 | AC | 743 ms
133,040 KB |
testcase_06 | AC | 734 ms
131,556 KB |
testcase_07 | AC | 743 ms
132,112 KB |
testcase_08 | AC | 450 ms
117,248 KB |
testcase_09 | AC | 479 ms
124,124 KB |
testcase_10 | AC | 752 ms
132,396 KB |
testcase_11 | AC | 760 ms
133,052 KB |
testcase_12 | AC | 439 ms
118,400 KB |
testcase_13 | AC | 286 ms
85,092 KB |
testcase_14 | AC | 512 ms
148,112 KB |
testcase_15 | AC | 475 ms
129,280 KB |
testcase_16 | AC | 282 ms
88,276 KB |
testcase_17 | AC | 380 ms
113,408 KB |
testcase_18 | AC | 314 ms
110,812 KB |
testcase_19 | AC | 375 ms
97,536 KB |
testcase_20 | AC | 506 ms
150,912 KB |
testcase_21 | AC | 98 ms
76,800 KB |
testcase_22 | AC | 385 ms
115,456 KB |
testcase_23 | AC | 415 ms
123,520 KB |
testcase_24 | AC | 282 ms
110,592 KB |
testcase_25 | AC | 508 ms
150,864 KB |
testcase_26 | AC | 425 ms
128,036 KB |
testcase_27 | AC | 495 ms
131,892 KB |
testcase_28 | AC | 471 ms
126,688 KB |
testcase_29 | AC | 450 ms
139,904 KB |
testcase_30 | AC | 288 ms
97,024 KB |
testcase_31 | AC | 254 ms
81,952 KB |
testcase_32 | AC | 255 ms
98,176 KB |
testcase_33 | AC | 274 ms
99,112 KB |
testcase_34 | AC | 454 ms
120,536 KB |
testcase_35 | AC | 131 ms
77,440 KB |
testcase_36 | AC | 452 ms
130,860 KB |
testcase_37 | AC | 402 ms
117,504 KB |
testcase_38 | AC | 188 ms
84,224 KB |
testcase_39 | AC | 303 ms
102,144 KB |
testcase_40 | AC | 200 ms
77,812 KB |
testcase_41 | AC | 342 ms
96,768 KB |
testcase_42 | AC | 32 ms
52,608 KB |
testcase_43 | AC | 31 ms
52,096 KB |
testcase_44 | AC | 30 ms
52,480 KB |
testcase_45 | AC | 438 ms
150,436 KB |
testcase_46 | AC | 445 ms
150,192 KB |
testcase_47 | AC | 452 ms
149,820 KB |
testcase_48 | AC | 450 ms
150,208 KB |
ソースコード
MOD = 10 ** 9 + 7 class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n self.group = 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 self.group -= 1 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 self.group def all_group_members(self): dic = {r:[] for r in self.roots()} for i in range(self.n): dic[self.find(i)].append(i) return dic def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) n, m, X = map(int, input().split()) UF = UnionFind(n) edges = [[] for _ in range(n)] for _ in range(m): x, y, z = map(int, input().split()) x -= 1 y -= 1 if not UF.same(x, y): UF.union(x, y) edges[x].append((y, z)) edges[y].append((x, z)) ans = 0 route = [0] stack = [0] used = [False] * n used[0] = True while stack: pos = stack.pop() for npos, _ in edges[pos]: if used[npos]: continue used[npos] = True stack.append(npos) route.append(npos) size = [1] * n used = [False] * n for pos in route[::-1]: used[pos] = True for npos, z in edges[pos]: if not used[npos]: continue ans += pow(X, z, MOD) * size[npos] * (n - size[npos]) ans %= MOD size[pos] += size[npos] print(ans)