diff --git a/minimum-changes-on-bipartite-coloring/sol-cpy-ac/main.py b/minimum-changes-on-bipartite-coloring/sol-cpy-ac/main.py index d01e86c..a0c1954 100644 --- a/minimum-changes-on-bipartite-coloring/sol-cpy-ac/main.py +++ b/minimum-changes-on-bipartite-coloring/sol-cpy-ac/main.py @@ -1,7 +1,76 @@ #!/usr/bin/python3 from sys import stdin -from atcoder.dsu import DSU + +import typing + + +# ac-library python +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)) def main(): diff --git a/minimum-changes-on-bipartite-coloring/sol-pypy-ac/main.py b/minimum-changes-on-bipartite-coloring/sol-pypy-ac/main.py index d7708d8..dc98bba 100644 --- a/minimum-changes-on-bipartite-coloring/sol-pypy-ac/main.py +++ b/minimum-changes-on-bipartite-coloring/sol-pypy-ac/main.py @@ -1,7 +1,76 @@ #!/usr/bin/env pypy from sys import stdin -from atcoder.dsu import DSU + +import typing + + +# ac-library python +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)) def main(): diff --git a/requirements.lock b/requirements.lock index 48eb0f0..7511c4f 100644 --- a/requirements.lock +++ b/requirements.lock @@ -1,4 +1,3 @@ -ac-library-python==0.0.1 bitarray==2.9.2 certifi==2024.2.2 charset-normalizer==3.3.2 diff --git a/requirements.txt b/requirements.txt index 6976cf5..1c489a3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ rime~=2.1.0 -git+https://github.com/not522/ac-library-python numpy scipy networkx