結果
問題 | No.1390 Get together |
ユーザー | 👑 terry_u16 |
提出日時 | 2021-02-12 22:05:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 417 ms / 2,000 ms |
コード長 | 2,160 bytes |
コンパイル時間 | 900 ms |
コンパイル使用メモリ | 87,296 KB |
実行使用メモリ | 87,624 KB |
最終ジャッジ日時 | 2023-09-27 04:08:24 |
合計ジャッジ時間 | 11,388 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 162 ms
80,096 KB |
testcase_01 | AC | 165 ms
80,232 KB |
testcase_02 | AC | 161 ms
80,100 KB |
testcase_03 | AC | 195 ms
82,396 KB |
testcase_04 | AC | 198 ms
82,120 KB |
testcase_05 | AC | 199 ms
82,340 KB |
testcase_06 | AC | 201 ms
82,496 KB |
testcase_07 | AC | 193 ms
82,476 KB |
testcase_08 | AC | 196 ms
82,228 KB |
testcase_09 | AC | 200 ms
82,116 KB |
testcase_10 | AC | 159 ms
80,048 KB |
testcase_11 | AC | 161 ms
80,148 KB |
testcase_12 | AC | 158 ms
80,112 KB |
testcase_13 | AC | 162 ms
80,108 KB |
testcase_14 | AC | 161 ms
80,120 KB |
testcase_15 | AC | 158 ms
80,048 KB |
testcase_16 | AC | 302 ms
85,488 KB |
testcase_17 | AC | 297 ms
85,364 KB |
testcase_18 | AC | 302 ms
85,404 KB |
testcase_19 | AC | 410 ms
86,504 KB |
testcase_20 | AC | 408 ms
86,552 KB |
testcase_21 | AC | 417 ms
86,804 KB |
testcase_22 | AC | 352 ms
86,196 KB |
testcase_23 | AC | 353 ms
85,620 KB |
testcase_24 | AC | 348 ms
86,176 KB |
testcase_25 | AC | 375 ms
86,096 KB |
testcase_26 | AC | 370 ms
85,992 KB |
testcase_27 | AC | 394 ms
86,160 KB |
testcase_28 | AC | 393 ms
85,876 KB |
testcase_29 | AC | 391 ms
87,624 KB |
testcase_30 | AC | 395 ms
86,792 KB |
testcase_31 | AC | 400 ms
86,216 KB |
ソースコード
import typing class DSU: ''' Implement (union by size) + (path halving) Reference: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems ''' def __init__(self, n: int = 0) -> None: self._n = n self.parent_or_size = [-1] * n def merge(self, a: int, b: int) -> int: assert 0 <= a < self._n assert 0 <= b < self._n x = self.leader(a) y = 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: int, b: int) -> bool: assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a: int) -> int: assert 0 <= a < self._n parent = self.parent_or_size[a] while parent >= 0: if self.parent_or_size[parent] < 0: return parent self.parent_or_size[a], a, parent = ( self.parent_or_size[parent], self.parent_or_size[parent], self.parent_or_size[self.parent_or_size[parent]] ) return a def size(self, a: int) -> int: assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self) -> typing.List[typing.List[int]]: leader_buf = [self.leader(i) for i in range(self._n)] result: typing.List[typing.List[int]] = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return list(filter(lambda r: r, result)) slime_count, box_count = map(int, input().split()) result = 0 slime_set = set() last = [-1] * slime_count uf = DSU(box_count) for _ in range(slime_count): box, color = map(int, input().split()) box -= 1 color -= 1 if last[color] != -1 and not uf.same(last[color], box): uf.merge(last[color], box) result += 1 last[color] = box print(result)