結果
問題 | No.1767 BLUE to RED |
ユーザー | Navier_Boltzmann |
提出日時 | 2021-11-27 18:14:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,394 ms / 2,000 ms |
コード長 | 1,551 bytes |
コンパイル時間 | 241 ms |
コンパイル使用メモリ | 82,484 KB |
実行使用メモリ | 177,272 KB |
最終ジャッジ日時 | 2024-06-30 09:06:14 |
合計ジャッジ時間 | 20,757 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
52,224 KB |
testcase_01 | AC | 37 ms
52,224 KB |
testcase_02 | AC | 37 ms
52,224 KB |
testcase_03 | AC | 38 ms
52,352 KB |
testcase_04 | AC | 47 ms
61,440 KB |
testcase_05 | AC | 48 ms
62,208 KB |
testcase_06 | AC | 38 ms
52,736 KB |
testcase_07 | AC | 59 ms
68,736 KB |
testcase_08 | AC | 63 ms
69,364 KB |
testcase_09 | AC | 805 ms
124,972 KB |
testcase_10 | AC | 1,006 ms
136,952 KB |
testcase_11 | AC | 880 ms
131,344 KB |
testcase_12 | AC | 817 ms
124,916 KB |
testcase_13 | AC | 1,116 ms
164,560 KB |
testcase_14 | AC | 1,376 ms
177,016 KB |
testcase_15 | AC | 1,394 ms
176,740 KB |
testcase_16 | AC | 1,394 ms
175,476 KB |
testcase_17 | AC | 1,356 ms
175,204 KB |
testcase_18 | AC | 1,362 ms
175,500 KB |
testcase_19 | AC | 1,351 ms
177,272 KB |
testcase_20 | AC | 1,369 ms
174,928 KB |
testcase_21 | AC | 1,377 ms
175,456 KB |
testcase_22 | AC | 1,365 ms
175,720 KB |
testcase_23 | AC | 1,359 ms
176,740 KB |
ソースコード
N,M = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x return x def same(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a): assert 0 <= a < self._n if self.parent_or_size[a] < 0: return a self.parent_or_size[a] = self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self, a): assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf = [self.leader(i) for i in range(self._n)] result = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return [r for r in result if r != []] X = A+B X.sort() SA = set(A) ans = 0 d = DSU(N+M+1) for i in range(N+M): if X[i] in SA: d.merge(i,N+M) edges = [(X[i+1]-X[i],i) for i in range(N+M-1)] edges.sort() for i in range(N+M-1): c,x = edges[i] if d.same(x,x+1): continue ans += c d.merge(x,x+1) print(ans)