結果
問題 | No.2946 Puyo |
ユーザー | 寝癖 |
提出日時 | 2024-10-25 21:40:53 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 580 ms / 2,000 ms |
コード長 | 2,258 bytes |
コンパイル時間 | 258 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 267,464 KB |
最終ジャッジ日時 | 2024-10-25 21:41:14 |
合計ジャッジ時間 | 17,229 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 61 ms
67,936 KB |
testcase_01 | AC | 61 ms
68,840 KB |
testcase_02 | AC | 61 ms
67,892 KB |
testcase_03 | AC | 580 ms
267,464 KB |
testcase_04 | AC | 573 ms
266,968 KB |
testcase_05 | AC | 572 ms
266,904 KB |
testcase_06 | AC | 542 ms
266,996 KB |
testcase_07 | AC | 577 ms
267,008 KB |
testcase_08 | AC | 129 ms
87,572 KB |
testcase_09 | AC | 269 ms
145,552 KB |
testcase_10 | AC | 134 ms
86,428 KB |
testcase_11 | AC | 343 ms
192,036 KB |
testcase_12 | AC | 123 ms
87,836 KB |
testcase_13 | AC | 65 ms
71,000 KB |
testcase_14 | AC | 221 ms
100,460 KB |
testcase_15 | AC | 152 ms
83,604 KB |
testcase_16 | AC | 243 ms
110,752 KB |
testcase_17 | AC | 413 ms
183,932 KB |
testcase_18 | AC | 131 ms
80,412 KB |
testcase_19 | AC | 269 ms
116,156 KB |
testcase_20 | AC | 308 ms
127,996 KB |
testcase_21 | AC | 416 ms
154,856 KB |
testcase_22 | AC | 173 ms
88,384 KB |
testcase_23 | AC | 305 ms
119,016 KB |
testcase_24 | AC | 472 ms
166,472 KB |
testcase_25 | AC | 420 ms
155,948 KB |
testcase_26 | AC | 467 ms
164,420 KB |
testcase_27 | AC | 125 ms
79,668 KB |
testcase_28 | AC | 298 ms
128,488 KB |
testcase_29 | AC | 374 ms
153,320 KB |
testcase_30 | AC | 271 ms
120,712 KB |
testcase_31 | AC | 328 ms
135,480 KB |
testcase_32 | AC | 341 ms
138,472 KB |
testcase_33 | AC | 324 ms
131,464 KB |
testcase_34 | AC | 399 ms
159,512 KB |
testcase_35 | AC | 357 ms
147,408 KB |
testcase_36 | AC | 374 ms
153,988 KB |
testcase_37 | AC | 401 ms
164,544 KB |
testcase_38 | AC | 334 ms
148,476 KB |
testcase_39 | AC | 330 ms
145,256 KB |
testcase_40 | AC | 258 ms
131,252 KB |
testcase_41 | AC | 416 ms
183,372 KB |
testcase_42 | AC | 368 ms
171,176 KB |
testcase_43 | AC | 285 ms
128,264 KB |
testcase_44 | AC | 319 ms
140,584 KB |
testcase_45 | AC | 286 ms
130,344 KB |
testcase_46 | AC | 230 ms
108,076 KB |
testcase_47 | AC | 287 ms
128,100 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)) H, W = map(int, input().split()) G = [list(input()) for _ in range(H)] dsu = DSU(H * W) for i in range(H): for j in range(W): for ni, nj in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]: if 0 <= ni < H and 0 <= nj < W and G[i][j] == G[ni][nj]: dsu.merge(i * W + j, ni * W + nj) for g in dsu.groups(): if len(g) >= 4: for k in g: i, j = divmod(k, W) G[i][j] = '.' for i in range(H): print(''.join(G[i]))