結果

問題 No.2291 Union Find Estimate
ユーザー rlangevinrlangevin
提出日時 2023-05-05 22:19:04
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,848 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 99,200 KB
最終ジャッジ日時 2024-05-02 16:52:41
合計ジャッジ時間 4,326 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
57,728 KB
testcase_01 AC 56 ms
62,848 KB
testcase_02 AC 194 ms
77,568 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


W, H = map(int, input().split())
mod = 998244353
U = UnionFind(W)
num = list(range(10))
num = set(list(map(str, num)))
numlst = [-1] * W
flag = 1
ans = pow(10, W, mod)
invten = [1] * (W + 1)
inv = pow(10, mod - 2, mod)
for i in range(W):
    invten[i + 1] = invten[i] * inv
    invten[i + 1] %= mod
    
for _ in range(H):
    S = list(input().rstrip("\n"))
    L = [[] for i in range(26)]
    cnt = 0
    for i, s in enumerate(S):
        if s == "?":
            pass
        elif s in num:
            s = int(s)
            if numlst[U.find(i)] == -1:
                cnt += 1
                numlst[U.find(i)] = s
            elif numlst[U.find(i)] != s:
                flag = 0
        else:           
            L[ord(s) - ord("a")].append(i)
            for j in range(26):
                if len(L[j]) <= 1:
                    continue
                else:
                    for k in range(1, len(L[j])):
                        if U.is_same(L[j][0], L[j][k]):
                            continue
                        else:
                            cnt += 1
                            Lj0 = U.find(L[j][0])
                            Ljk = U.find(L[j][k])
                            if numlst[Lj0] != numlst[Ljk]:
                                if numlst[Lj0] != -1 and numlst[Ljk] != -1:
                                    flag = 0
                                else:
                                    mx = max(numlst[Lj0], numlst[Ljk])
                                    U.union(Lj0, Ljk)
                                    numlst[U.find(Lj0)] = mx
                            else:
                                if numlst[Lj0] != -1:
                                    cnt -= 1
                                mx = max(numlst[Lj0], numlst[Ljk])
                                U.union(Lj0, Ljk)
                                numlst[U.find(Lj0)] = mx
    ans *= invten[cnt]
    ans %= mod
    if flag:
        print(ans)
    else:
        print(0)
0