結果

問題 No.2291 Union Find Estimate
ユーザー t98slidert98slider
提出日時 2023-03-16 02:09:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 523 ms / 2,000 ms
コード長 1,434 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 79,068 KB
最終ジャッジ日時 2023-10-18 12:47:10
合計ジャッジ時間 3,216 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,604 KB
testcase_01 AC 38 ms
53,604 KB
testcase_02 AC 523 ms
75,952 KB
testcase_03 AC 64 ms
79,068 KB
testcase_04 AC 48 ms
57,700 KB
testcase_05 AC 50 ms
63,192 KB
testcase_06 AC 44 ms
55,652 KB
testcase_07 AC 43 ms
59,036 KB
testcase_08 AC 64 ms
70,676 KB
testcase_09 AC 67 ms
70,616 KB
testcase_10 AC 99 ms
75,964 KB
testcase_11 AC 148 ms
76,372 KB
testcase_12 AC 195 ms
76,380 KB
testcase_13 AC 56 ms
67,032 KB
testcase_14 AC 88 ms
75,744 KB
testcase_15 AC 75 ms
77,444 KB
testcase_16 AC 81 ms
75,980 KB
testcase_17 AC 67 ms
72,584 KB
testcase_18 AC 65 ms
70,528 KB
testcase_19 AC 100 ms
75,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    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: return
        if 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)

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

for _ in range(H):
    s = input()

    if is_zero == 1:
        print(0)
        continue

    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

    for i in range(10):
        for j in range(i + 1, 10):
            if uf.same(i + W, j + W): is_zero = 1
    
    if is_zero:
        print(0)
    else:
        print(pow10[uf.siz])
0