結果
問題 | No.2200 Weird Shortest Path |
ユーザー | H20 |
提出日時 | 2023-01-28 19:35:23 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,400 ms / 2,000 ms |
コード長 | 1,713 bytes |
コンパイル時間 | 178 ms |
コンパイル使用メモリ | 82,440 KB |
実行使用メモリ | 129,336 KB |
最終ジャッジ日時 | 2024-06-28 22:04:53 |
合計ジャッジ時間 | 33,765 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
55,052 KB |
testcase_01 | AC | 39 ms
56,612 KB |
testcase_02 | AC | 39 ms
55,684 KB |
testcase_03 | AC | 64 ms
70,452 KB |
testcase_04 | AC | 105 ms
77,888 KB |
testcase_05 | AC | 41 ms
56,016 KB |
testcase_06 | AC | 48 ms
63,736 KB |
testcase_07 | AC | 59 ms
69,076 KB |
testcase_08 | AC | 97 ms
77,376 KB |
testcase_09 | AC | 93 ms
77,256 KB |
testcase_10 | AC | 41 ms
55,500 KB |
testcase_11 | AC | 43 ms
55,140 KB |
testcase_12 | AC | 46 ms
56,032 KB |
testcase_13 | AC | 40 ms
56,336 KB |
testcase_14 | AC | 41 ms
55,284 KB |
testcase_15 | AC | 96 ms
77,476 KB |
testcase_16 | AC | 41 ms
55,164 KB |
testcase_17 | AC | 508 ms
93,992 KB |
testcase_18 | AC | 803 ms
105,096 KB |
testcase_19 | AC | 1,227 ms
126,756 KB |
testcase_20 | AC | 1,242 ms
124,832 KB |
testcase_21 | AC | 579 ms
96,016 KB |
testcase_22 | AC | 733 ms
103,068 KB |
testcase_23 | AC | 1,400 ms
126,904 KB |
testcase_24 | AC | 414 ms
86,588 KB |
testcase_25 | AC | 1,235 ms
126,008 KB |
testcase_26 | AC | 1,290 ms
127,292 KB |
testcase_27 | AC | 573 ms
95,808 KB |
testcase_28 | AC | 973 ms
114,980 KB |
testcase_29 | AC | 1,299 ms
127,364 KB |
testcase_30 | AC | 1,278 ms
126,800 KB |
testcase_31 | AC | 880 ms
108,432 KB |
testcase_32 | AC | 1,345 ms
128,264 KB |
testcase_33 | AC | 1,329 ms
127,852 KB |
testcase_34 | AC | 1,302 ms
127,356 KB |
testcase_35 | AC | 1,320 ms
127,700 KB |
testcase_36 | AC | 1,308 ms
128,412 KB |
testcase_37 | AC | 1,308 ms
127,724 KB |
testcase_38 | AC | 1,308 ms
128,736 KB |
testcase_39 | AC | 746 ms
103,528 KB |
testcase_40 | AC | 836 ms
126,536 KB |
testcase_41 | AC | 499 ms
100,796 KB |
testcase_42 | AC | 815 ms
129,336 KB |
testcase_43 | AC | 1,224 ms
123,508 KB |
testcase_44 | AC | 1,188 ms
124,000 KB |
testcase_45 | AC | 1,219 ms
124,312 KB |
ソースコード
# UnionFind 参考は以下のサイト # https://note.nkmk.me/python-union-find/ from collections import defaultdict import heapq 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): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) N,M = map(int, input().split()) ABW = [list(map(int, input().split())) for _ in range(M)] H = [] for a,b,w in ABW: heapq.heappush(H,(w,a,b)) uf = UnionFind(N) ans = 0 while H: w,a,b = heapq.heappop(H) a-=1 b-=1 if not uf.same(a,b): ans += uf.size(a)*uf.size(b)*w uf.union(a,b) print(ans)