結果
問題 | No.1207 グラフX |
ユーザー | WSKR |
提出日時 | 2020-09-22 22:26:41 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,422 bytes |
コンパイル時間 | 251 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 144,404 KB |
最終ジャッジ日時 | 2024-06-26 22:11:48 |
合計ジャッジ時間 | 29,763 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 667 ms
143,296 KB |
testcase_01 | AC | 665 ms
143,148 KB |
testcase_02 | AC | 661 ms
142,668 KB |
testcase_03 | AC | 676 ms
142,700 KB |
testcase_04 | AC | 680 ms
142,972 KB |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | AC | 637 ms
117,424 KB |
testcase_09 | AC | 656 ms
127,964 KB |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | AC | 624 ms
121,128 KB |
testcase_13 | AC | 402 ms
87,804 KB |
testcase_14 | AC | 721 ms
142,340 KB |
testcase_15 | AC | 665 ms
127,296 KB |
testcase_16 | AC | 413 ms
91,136 KB |
testcase_17 | AC | 543 ms
112,988 KB |
testcase_18 | AC | 443 ms
111,712 KB |
testcase_19 | AC | 523 ms
99,840 KB |
testcase_20 | AC | 695 ms
143,420 KB |
testcase_21 | AC | 134 ms
76,800 KB |
testcase_22 | AC | 549 ms
113,904 KB |
testcase_23 | AC | 576 ms
120,960 KB |
testcase_24 | AC | 407 ms
108,672 KB |
testcase_25 | AC | 698 ms
144,404 KB |
testcase_26 | AC | 597 ms
124,500 KB |
testcase_27 | AC | 671 ms
134,732 KB |
testcase_28 | AC | 664 ms
128,672 KB |
testcase_29 | AC | 643 ms
134,088 KB |
testcase_30 | AC | 425 ms
98,816 KB |
testcase_31 | AC | 368 ms
84,352 KB |
testcase_32 | AC | 373 ms
100,864 KB |
testcase_33 | AC | 404 ms
99,500 KB |
testcase_34 | AC | 658 ms
125,420 KB |
testcase_35 | AC | 180 ms
77,952 KB |
testcase_36 | AC | 627 ms
127,216 KB |
testcase_37 | AC | 572 ms
117,468 KB |
testcase_38 | AC | 276 ms
85,632 KB |
testcase_39 | AC | 403 ms
102,016 KB |
testcase_40 | AC | 269 ms
79,372 KB |
testcase_41 | AC | 509 ms
100,608 KB |
testcase_42 | AC | 40 ms
52,608 KB |
testcase_43 | AC | 40 ms
52,352 KB |
testcase_44 | AC | 41 ms
52,608 KB |
testcase_45 | AC | 640 ms
142,544 KB |
testcase_46 | AC | 645 ms
142,564 KB |
testcase_47 | AC | 643 ms
142,308 KB |
testcase_48 | AC | 641 ms
142,312 KB |
ソースコード
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()