結果
問題 | No.2838 Diagonals |
ユーザー | Butterflv |
提出日時 | 2024-08-09 23:23:24 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,406 bytes |
コンパイル時間 | 260 ms |
コンパイル使用メモリ | 82,604 KB |
実行使用メモリ | 81,912 KB |
最終ジャッジ日時 | 2024-08-09 23:23:28 |
合計ジャッジ時間 | 4,219 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | AC | 56 ms
67,904 KB |
testcase_02 | WA | - |
testcase_03 | AC | 56 ms
67,936 KB |
testcase_04 | AC | 60 ms
67,736 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 148 ms
80,956 KB |
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 S[h-1][w]=='#'and S[h][w-1]=='#': if uf.same((h-1)*M+w, h*M+w-1): ans*=4 else: ans*=2 else: ans*=4 ans%=998244353 print(ans)