結果
問題 | No.860 買い物 |
ユーザー | takeya_okino |
提出日時 | 2021-09-26 13:21:27 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,589 bytes |
コンパイル時間 | 274 ms |
コンパイル使用メモリ | 86,440 KB |
実行使用メモリ | 106,808 KB |
最終ジャッジ日時 | 2023-09-19 02:52:19 |
合計ジャッジ時間 | 11,392 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 99 ms
71,528 KB |
testcase_01 | AC | 100 ms
71,328 KB |
testcase_02 | AC | 96 ms
71,460 KB |
testcase_03 | AC | 96 ms
71,200 KB |
testcase_04 | AC | 98 ms
71,508 KB |
testcase_05 | AC | 99 ms
71,204 KB |
testcase_06 | AC | 176 ms
78,996 KB |
testcase_07 | AC | 956 ms
104,348 KB |
testcase_08 | AC | 933 ms
103,728 KB |
testcase_09 | AC | 957 ms
103,736 KB |
testcase_10 | AC | 960 ms
103,568 KB |
testcase_11 | AC | 379 ms
103,580 KB |
testcase_12 | AC | 372 ms
101,832 KB |
testcase_13 | AC | 880 ms
104,600 KB |
testcase_14 | TLE | - |
testcase_15 | AC | 413 ms
101,584 KB |
testcase_16 | AC | 912 ms
106,808 KB |
testcase_17 | AC | 357 ms
101,184 KB |
ソースコード
n = int(input()) A = [] ans = 0 for i in range(n): c, d = map(int, input().split()) ans += c A.append([c, i, n]) if i > 0: A.append([d, i - 1, i]) A.sort() from collections import defaultdict 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()) uf = UnionFind(n + 1) for [c, a, b] in A: if not uf.same(a,b): ans += c uf.union(a,b) print(ans)