結果
問題 | No.2838 Diagonals |
ユーザー | Butterflv |
提出日時 | 2024-08-09 23:20:14 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,367 bytes |
コンパイル時間 | 321 ms |
コンパイル使用メモリ | 82,292 KB |
実行使用メモリ | 82,336 KB |
最終ジャッジ日時 | 2024-08-09 23:20:18 |
合計ジャッジ時間 | 4,470 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 58 ms
68,272 KB |
testcase_01 | AC | 58 ms
67,980 KB |
testcase_02 | WA | - |
testcase_03 | AC | 56 ms
68,956 KB |
testcase_04 | AC | 57 ms
67,472 KB |
testcase_05 | AC | 56 ms
68,384 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | AC | 140 ms
80,656 KB |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
ソースコード
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()) S=[input() for i in range(N)] uf=DSU(N*M) dh=(1,-1,0,0,) dw=(0,0,1,-1,) for h in range(N): for w in range(M): for k in range(4): nh=h+dh[k]; nw=w+dw[k] if((0<=nh<N)and(0<=nw<M))==False: continue if S[h][w]=='#'and S[nh][nw]=='#': uf.merge(h*M+w, nh*M+nw) ans=1 for h in range(N): for w in range(M): if h==0 or w==0: if S[h][w]=='#': ans*=4 continue if S[h][w]=='.': continue if uf.same(h*M+w, (h-1)*M+w)and uf.same(h*M+w, h*M+w-1): ans*=2 else: ans*=4 ans%=998244353 print(ans)