結果

問題 No.2291 Union Find Estimate
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-05-05 21:59:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 490 ms / 2,000 ms
コード長 1,758 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 79,740 KB
最終ジャッジ日時 2023-08-15 04:01:24
合計ジャッジ時間 4,116 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,428 KB
testcase_01 AC 75 ms
71,360 KB
testcase_02 AC 490 ms
78,460 KB
testcase_03 AC 89 ms
79,740 KB
testcase_04 AC 120 ms
77,644 KB
testcase_05 AC 128 ms
77,768 KB
testcase_06 AC 117 ms
77,532 KB
testcase_07 AC 103 ms
77,196 KB
testcase_08 AC 120 ms
77,748 KB
testcase_09 AC 120 ms
77,536 KB
testcase_10 AC 130 ms
77,436 KB
testcase_11 AC 149 ms
77,272 KB
testcase_12 AC 196 ms
77,292 KB
testcase_13 AC 107 ms
78,136 KB
testcase_14 AC 113 ms
77,384 KB
testcase_15 AC 107 ms
79,036 KB
testcase_16 AC 117 ms
77,252 KB
testcase_17 AC 112 ms
77,624 KB
testcase_18 AC 109 ms
77,112 KB
testcase_19 AC 133 ms
77,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
        self.ps = [-2]*n
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
    
    def update(self,x,nx):

        x = self.find(x)

        ny = self.ps[x]

        if nx != ny:
            if nx != -2 and ny != -2:
                self.ps[x] = -1
            else:
                self.ps[x] = max(nx,ny)


    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        nx = self.ps[x]
        ny = self.ps[y]
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]

        self.uf[y] = x
        if nx != ny:
            if nx != -2 and ny != -2:
                self.ps[x] = -1
            else:
                self.ps[x] = max(nx,ny)

        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]



w,h = map(int,input().split())
mod = 998244353
uf = Unionfind(w)
for i in range(h):
    q = input()
    last = [-1]*26
    for j in range(w):
        s = q[j]
        if "0" <= s <= "9":
            uf.update(j,int(s))
        elif "a" <= s <= "z":
            pos = ord(s)-ord("a")
            if last[pos] == -1:
                last[pos] = j
            else:
                uf.union(last[pos],j)

    
    ans = 1
    for j in range(w):
        if uf.find(j) == j:
            num = uf.ps[j]
            if num == -2:
                ans *= 10
            elif num == -1:
                ans = 0
                break
            ans %= mod
    print(ans)

0