結果
問題 | No.1207 グラフX |
ユーザー | WSKR |
提出日時 | 2020-09-22 22:27:35 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,462 bytes |
コンパイル時間 | 1,395 ms |
コンパイル使用メモリ | 86,428 KB |
実行使用メモリ | 841,352 KB |
最終ジャッジ日時 | 2023-09-09 05:06:49 |
合計ジャッジ時間 | 17,861 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 767 ms
144,004 KB |
testcase_01 | AC | 685 ms
144,132 KB |
testcase_02 | AC | 673 ms
144,736 KB |
testcase_03 | AC | 687 ms
144,896 KB |
testcase_04 | AC | 685 ms
143,964 KB |
testcase_05 | AC | 1,727 ms
420,288 KB |
testcase_06 | AC | 1,613 ms
397,700 KB |
testcase_07 | AC | 1,642 ms
416,496 KB |
testcase_08 | AC | 661 ms
119,128 KB |
testcase_09 | AC | 684 ms
131,320 KB |
testcase_10 | MLE | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
ソースコード
import sys sys.setrecursionlimit(10**7) class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * (n+1) 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 + 1) 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()) mod = 10 ** 9 + 7 def main(): N, M, X = map(int, input().split()) weight_cost = [0 for i in range(N+1)] Uni = UnionFind(N) res = 0 que = [] G = [[] for i in range(N+1)] for i in range(M): x, y, z = map(int, input().split()) if Uni.same(x, y) == True: continue else: Uni.union(x, y) G[x].append(y) G[y].append(x) que.append((x, y, z)) #ここで得られる構造は木構造 visited = [False for i in range(N+1)] depth = [0 for i in range(N+1)] child_cnt = [1 for i in range(N+1)] def dfs(s_node): visited[s_node] = True for node in G[s_node]: if visited[node] == False: depth[node] = depth[s_node] + 1 dfs(node) for node in G[s_node]: if depth[node] > depth[s_node]: child_cnt[s_node] += child_cnt[node] dfs(1) for x, y, z in que: if depth[x] > depth[y]: res += pow(X, z, mod) * child_cnt[x]*(N-child_cnt[x]) res %= mod else: res += pow(X, z, mod) * child_cnt[y]*(N-child_cnt[y]) res %= mod print(res) return if __name__ == "__main__": main()