結果

問題 No.2291 Union Find Estimate
ユーザー t98slidert98slider
提出日時 2023-03-16 19:29:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 1,473 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 78,652 KB
最終ジャッジ日時 2023-10-18 13:09:15
合計ジャッジ時間 2,278 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,372 KB
testcase_01 AC 37 ms
53,372 KB
testcase_02 AC 113 ms
76,424 KB
testcase_03 AC 64 ms
78,652 KB
testcase_04 AC 41 ms
53,372 KB
testcase_05 AC 40 ms
53,372 KB
testcase_06 AC 40 ms
53,372 KB
testcase_07 AC 40 ms
53,372 KB
testcase_08 AC 46 ms
59,604 KB
testcase_09 AC 46 ms
61,652 KB
testcase_10 AC 62 ms
74,060 KB
testcase_11 AC 71 ms
75,656 KB
testcase_12 AC 77 ms
75,660 KB
testcase_13 AC 57 ms
66,804 KB
testcase_14 AC 72 ms
75,680 KB
testcase_15 AC 77 ms
77,340 KB
testcase_16 AC 82 ms
75,772 KB
testcase_17 AC 64 ms
68,260 KB
testcase_18 AC 64 ms
70,308 KB
testcase_19 AC 65 ms
74,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin

class UnionFind():
    def __init__(self, n):
        self.siz = n - 10
        self.parent_or_size = [-1] * n
        self.is_zero = False

    def leader(self, x):
        if self.parent_or_size[x] < 0: return x
        self.parent_or_size[x] = self.leader(self.parent_or_size[x])
        return self.parent_or_size[x]

    def same(self, x, y):
        return self.leader(x) == self.leader(y)

    def merge(self, x, y):
        rx, ry = self.leader(x), self.leader(y)
        if rx < ry: rx, ry = ry, rx
        if rx == ry: return
        if ry >= len(self.parent_or_size) - 10: self.is_zero = True
        elif rx < len(self.parent_or_size) - 10 & self.parent_or_size[rx] > self.parent_or_size[ry]: rx, ry = ry, rx
        self.parent_or_size[rx] += self.parent_or_size[ry]
        self.parent_or_size[ry] = rx
        self.siz -= 1
        return

W, H = map(int, input().split())

uf = UnionFind(W + 10)

pow10 = [0] * (W + 1)
pow10[0] = 1
for i in range(W):  pow10[i + 1] = (pow10[i] * 10) % 998244353

for _ in range(H):
    if uf.is_zero:
        print(0)
        continue
    
    s = stdin.readline()

    pos = [-1] * 26

    for i in range(W):
        if s[i] == '?': continue
        if '0' <= s[i] and s[i] <= '9':
            uf.merge(ord(s[i]) - ord('0') + W, i)
            continue
        c = ord(s[i]) - ord('a')
        if pos[c] != -1: uf.merge(i, pos[c])
        pos[c] = i
    
    print(0 if uf.is_zero else pow10[uf.siz])
0