結果
問題 | No.2563 色ごとのグループ |
ユーザー | yaakiyu |
提出日時 | 2023-12-02 15:09:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 353 ms / 2,000 ms |
コード長 | 2,091 bytes |
コンパイル時間 | 236 ms |
コンパイル使用メモリ | 81,700 KB |
実行使用メモリ | 134,592 KB |
最終ジャッジ日時 | 2023-12-02 15:09:15 |
合計ジャッジ時間 | 6,724 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
68,232 KB |
testcase_01 | AC | 54 ms
68,232 KB |
testcase_02 | AC | 55 ms
68,232 KB |
testcase_03 | AC | 56 ms
68,232 KB |
testcase_04 | AC | 58 ms
68,232 KB |
testcase_05 | AC | 54 ms
68,232 KB |
testcase_06 | AC | 54 ms
68,232 KB |
testcase_07 | AC | 54 ms
68,232 KB |
testcase_08 | AC | 56 ms
68,232 KB |
testcase_09 | AC | 60 ms
68,232 KB |
testcase_10 | AC | 57 ms
68,232 KB |
testcase_11 | AC | 58 ms
68,232 KB |
testcase_12 | AC | 56 ms
68,232 KB |
testcase_13 | AC | 54 ms
68,232 KB |
testcase_14 | AC | 67 ms
74,184 KB |
testcase_15 | AC | 70 ms
74,440 KB |
testcase_16 | AC | 69 ms
74,448 KB |
testcase_17 | AC | 70 ms
74,320 KB |
testcase_18 | AC | 63 ms
70,960 KB |
testcase_19 | AC | 76 ms
78,152 KB |
testcase_20 | AC | 81 ms
79,400 KB |
testcase_21 | AC | 82 ms
78,244 KB |
testcase_22 | AC | 77 ms
78,128 KB |
testcase_23 | AC | 76 ms
78,244 KB |
testcase_24 | AC | 154 ms
97,968 KB |
testcase_25 | AC | 131 ms
100,020 KB |
testcase_26 | AC | 166 ms
113,560 KB |
testcase_27 | AC | 179 ms
134,592 KB |
testcase_28 | AC | 187 ms
126,368 KB |
testcase_29 | AC | 223 ms
133,792 KB |
testcase_30 | AC | 237 ms
133,788 KB |
testcase_31 | AC | 229 ms
133,788 KB |
testcase_32 | AC | 224 ms
133,788 KB |
testcase_33 | AC | 321 ms
117,252 KB |
testcase_34 | AC | 326 ms
117,652 KB |
testcase_35 | AC | 335 ms
117,592 KB |
testcase_36 | AC | 336 ms
117,412 KB |
testcase_37 | AC | 353 ms
117,776 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)))