結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,604 KB
testcase_01 AC 38 ms
53,604 KB
testcase_02 AC 188 ms
77,288 KB
testcase_03 AC 63 ms
78,876 KB
testcase_04 AC 41 ms
55,652 KB
testcase_05 AC 42 ms
55,652 KB
testcase_06 AC 41 ms
55,652 KB
testcase_07 AC 42 ms
59,036 KB
testcase_08 AC 56 ms
68,556 KB
testcase_09 AC 62 ms
70,616 KB
testcase_10 AC 72 ms
75,888 KB
testcase_11 AC 95 ms
76,300 KB
testcase_12 AC 109 ms
76,584 KB
testcase_13 AC 57 ms
67,032 KB
testcase_14 AC 82 ms
75,752 KB
testcase_15 AC 74 ms
77,448 KB
testcase_16 AC 81 ms
75,976 KB
testcase_17 AC 66 ms
70,536 KB
testcase_18 AC 63 ms
70,528 KB
testcase_19 AC 73 ms
75,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin

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 = stdin.readline()

    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