結果
問題 | No.157 2つの空洞 |
ユーザー | ntuda |
提出日時 | 2024-01-27 12:47:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 69 ms / 2,000 ms |
コード長 | 2,888 bytes |
コンパイル時間 | 219 ms |
コンパイル使用メモリ | 82,348 KB |
実行使用メモリ | 72,700 KB |
最終ジャッジ日時 | 2024-09-28 09:36:05 |
合計ジャッジ時間 | 2,330 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 64 ms
68,168 KB |
testcase_01 | AC | 61 ms
68,696 KB |
testcase_02 | AC | 60 ms
68,448 KB |
testcase_03 | AC | 62 ms
67,948 KB |
testcase_04 | AC | 62 ms
69,272 KB |
testcase_05 | AC | 63 ms
67,552 KB |
testcase_06 | AC | 69 ms
68,584 KB |
testcase_07 | AC | 60 ms
67,896 KB |
testcase_08 | AC | 63 ms
68,152 KB |
testcase_09 | AC | 61 ms
69,316 KB |
testcase_10 | AC | 62 ms
69,304 KB |
testcase_11 | AC | 64 ms
67,996 KB |
testcase_12 | AC | 64 ms
69,548 KB |
testcase_13 | AC | 65 ms
70,000 KB |
testcase_14 | AC | 63 ms
71,348 KB |
testcase_15 | AC | 68 ms
70,960 KB |
testcase_16 | AC | 66 ms
70,740 KB |
testcase_17 | AC | 68 ms
72,700 KB |
testcase_18 | AC | 65 ms
70,724 KB |
testcase_19 | AC | 67 ms
71,492 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)) W, H = map(int, input().split()) C = [input() for _ in range(H)] dsu = DSU(H * W) D = [] for h in range(H): for w in range(W): hw = h * W + w if C[h][w] == ".": D.append(hw) if h > 0: if C[h - 1][w] == ".": dsu.merge(hw, (h - 1) * W + w) if w > 0: if C[h][w - 1] == ".": dsu.merge(hw, hw - 1) l0 = dsu.leader(D[0]) l1 = -1 q = [] for d in D: if dsu.leader(d) == l0: q.append(d) else: l1 = dsu.leader(d) #print(sorted(q)) def merge(x): global q2 if not dsu.same(x, l0): dsu.merge(x, l0) q2.add(x) lead = [] cnt = 0 cnt = 0 while not dsu.same(l0,l1): q2 = set() while q: x = q.pop() if x >= W: y = x - W merge(y) if x < (H -1) * W: y = x + W merge(y) if x % W != 0: y = x - 1 merge(y) if x % W != W - 1: y = x + 1 merge(y) q = list(q2) #print(cnt,sorted(q)) cnt += 1 print(cnt-1)