結果

問題 No.2291 Union Find Estimate
ユーザー rlangevinrlangevin
提出日時 2023-05-05 22:21:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 2,656 bytes
コンパイル時間 435 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 107,392 KB
最終ジャッジ日時 2024-05-02 16:57:27
合計ジャッジ時間 3,698 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,736 KB
testcase_01 AC 45 ms
52,736 KB
testcase_02 AC 209 ms
77,312 KB
testcase_03 AC 119 ms
107,392 KB
testcase_04 AC 136 ms
76,928 KB
testcase_05 AC 134 ms
76,800 KB
testcase_06 AC 123 ms
76,800 KB
testcase_07 AC 111 ms
76,416 KB
testcase_08 AC 108 ms
76,672 KB
testcase_09 AC 103 ms
77,056 KB
testcase_10 AC 114 ms
76,800 KB
testcase_11 AC 122 ms
76,800 KB
testcase_12 AC 151 ms
77,312 KB
testcase_13 AC 104 ms
81,364 KB
testcase_14 AC 111 ms
76,416 KB
testcase_15 AC 143 ms
90,736 KB
testcase_16 AC 123 ms
78,244 KB
testcase_17 AC 108 ms
77,696 KB
testcase_18 AC 109 ms
77,312 KB
testcase_19 AC 96 ms
77,056 KB
権限があれば一括ダウンロードができます

ソースコード

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