結果
問題 | No.2563 色ごとのグループ |
ユーザー | yaakiyu |
提出日時 | 2023-12-02 15:09:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 351 ms / 2,000 ms |
コード長 | 2,091 bytes |
コンパイル時間 | 368 ms |
コンパイル使用メモリ | 82,516 KB |
実行使用メモリ | 134,880 KB |
最終ジャッジ日時 | 2024-09-26 18:10:12 |
合計ジャッジ時間 | 7,126 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
67,072 KB |
testcase_01 | AC | 58 ms
67,200 KB |
testcase_02 | AC | 58 ms
67,328 KB |
testcase_03 | AC | 58 ms
67,200 KB |
testcase_04 | AC | 58 ms
67,072 KB |
testcase_05 | AC | 58 ms
66,944 KB |
testcase_06 | AC | 55 ms
67,200 KB |
testcase_07 | AC | 57 ms
67,436 KB |
testcase_08 | AC | 56 ms
67,072 KB |
testcase_09 | AC | 58 ms
67,328 KB |
testcase_10 | AC | 56 ms
67,328 KB |
testcase_11 | AC | 56 ms
67,584 KB |
testcase_12 | AC | 57 ms
67,584 KB |
testcase_13 | AC | 58 ms
67,712 KB |
testcase_14 | AC | 73 ms
74,752 KB |
testcase_15 | AC | 71 ms
74,880 KB |
testcase_16 | AC | 74 ms
74,776 KB |
testcase_17 | AC | 72 ms
74,468 KB |
testcase_18 | AC | 66 ms
71,296 KB |
testcase_19 | AC | 81 ms
78,848 KB |
testcase_20 | AC | 83 ms
79,656 KB |
testcase_21 | AC | 81 ms
78,720 KB |
testcase_22 | AC | 78 ms
78,464 KB |
testcase_23 | AC | 81 ms
78,800 KB |
testcase_24 | AC | 142 ms
98,432 KB |
testcase_25 | AC | 135 ms
100,632 KB |
testcase_26 | AC | 165 ms
113,884 KB |
testcase_27 | AC | 183 ms
134,880 KB |
testcase_28 | AC | 190 ms
127,292 KB |
testcase_29 | AC | 225 ms
134,068 KB |
testcase_30 | AC | 226 ms
134,588 KB |
testcase_31 | AC | 231 ms
134,080 KB |
testcase_32 | AC | 226 ms
134,732 KB |
testcase_33 | AC | 351 ms
118,188 KB |
testcase_34 | AC | 327 ms
118,172 KB |
testcase_35 | AC | 324 ms
117,976 KB |
testcase_36 | AC | 332 ms
117,788 KB |
testcase_37 | AC | 350 ms
118,036 KB |
ソースコード
import sys input = lambda: sys.stdin.readline()[:-1] sys.setrecursionlimit(1000000) 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)) n, m = map(int, input().split()) c = tuple(map(int, input().split())) uf = DSU(n) for i in range(m): u, v = map(int, input().split()) if c[u-1] == c[v-1]: uf.merge(u-1, v-1) print(len(uf.groups()) - len(set(c)))