結果

問題 No.2291 Union Find Estimate
ユーザー ikomaikoma
提出日時 2023-05-05 23:20:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 1,612 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 76,672 KB
最終ジャッジ日時 2024-05-02 18:33:19
合計ジャッジ時間 3,372 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,224 KB
testcase_01 AC 38 ms
51,840 KB
testcase_02 AC 116 ms
76,160 KB
testcase_03 AC 65 ms
65,920 KB
testcase_04 AC 89 ms
75,904 KB
testcase_05 AC 93 ms
76,032 KB
testcase_06 AC 87 ms
76,160 KB
testcase_07 AC 73 ms
72,960 KB
testcase_08 AC 76 ms
75,904 KB
testcase_09 AC 79 ms
75,904 KB
testcase_10 AC 84 ms
76,032 KB
testcase_11 AC 87 ms
76,160 KB
testcase_12 AC 106 ms
76,416 KB
testcase_13 AC 75 ms
75,904 KB
testcase_14 AC 89 ms
75,904 KB
testcase_15 AC 93 ms
76,672 KB
testcase_16 AC 96 ms
75,648 KB
testcase_17 AC 81 ms
75,904 KB
testcase_18 AC 82 ms
75,648 KB
testcase_19 AC 77 ms
76,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
MOD=998244353
W,H=map(int,input().split())
class UnionFind:
    def __init__(self, n:int):
        self.n = n
        self.parents = [-1] * n
    def find(self, x:int):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x:int, y:int):
        x = self.find(x)
        y = self.find(y)
        if x == y: return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        if y<10:
            x,y=y,x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    def size(self, x:int):
        return -self.parents[self.find(x)]
    def same(self, x:int, y:int):
        return self.find(x) == self.find(y)

uf = UnionFind(10+W)
ans = pow(10, W, MOD)
div10 = pow(10, MOD-2, MOD)

for _ in range(H):
    Q=input().strip()
    check = {}
    for i,s in enumerate(Q,start=10):
        if s=="?":continue
        if "a"<=s<="z":
            if s in check:
                if uf.same(check[s], i):
                    continue
                ca=uf.find(check[s])
                ci=uf.find(i)
                if ca<10 and ci<10:
                    ans=0
                uf.union(check[s], i)
                ans = ans * div10 % MOD
            else:
                check[s] = i
        else:
            root = uf.find(i)
            s = int(s)
            if root < 10 and root != s:
                ans=0
            if root != s:
                uf.union(s, i)
                ans = ans * div10 % MOD
    print(ans)
0