結果

問題 No.2838 Diagonals
ユーザー titia
提出日時 2024-08-11 02:56:06
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 955 ms / 2,000 ms
コード長 1,138 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 22,912 KB
最終ジャッジ日時 2024-08-11 02:56:11
合計ジャッジ時間 4,636 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=998244353

N,M=map(int,input().split())
MAP=[input().strip() for i in range(N)]

# UnionFind

Group = [i for i in range(N*M+1)] # グループ分け
Nodes = [1]*(N*M+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

INV=pow(2,mod-2,mod)
ANS=1
for i in range(N):
    for j in range(M):
        if MAP[i][j]=="#":
            ANS=ANS*4%mod

            if i>0 and j>0 and MAP[i-1][j]=="#" and MAP[i][j-1]=="#":
                if find((i-1)*M+j)==find(i*M+j-1):
                    ANS=ANS*INV%mod
            if i>0 and MAP[i-1][j]=="#":
                Union(i*M+j,(i-1)*M+j)
            if j>0 and MAP[i][j-1]=="#":
                Union(i*M+j,i*M+j-1)

print(ANS)

            
0