結果

問題 No.2291 Union Find Estimate
ユーザー t98slidert98slider
提出日時 2023-03-16 19:25:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 500 ms / 2,000 ms
コード長 1,487 bytes
コンパイル時間 570 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 20,040 KB
最終ジャッジ日時 2023-10-18 13:09:10
合計ジャッジ時間 4,701 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,260 KB
testcase_01 AC 29 ms
10,260 KB
testcase_02 AC 500 ms
10,528 KB
testcase_03 AC 412 ms
20,040 KB
testcase_04 AC 168 ms
10,316 KB
testcase_05 AC 166 ms
10,320 KB
testcase_06 AC 106 ms
10,304 KB
testcase_07 AC 71 ms
10,300 KB
testcase_08 AC 58 ms
10,320 KB
testcase_09 AC 58 ms
10,316 KB
testcase_10 AC 72 ms
10,428 KB
testcase_11 AC 87 ms
10,500 KB
testcase_12 AC 116 ms
10,468 KB
testcase_13 AC 74 ms
12,256 KB
testcase_14 AC 290 ms
10,316 KB
testcase_15 AC 362 ms
15,228 KB
testcase_16 AC 306 ms
10,740 KB
testcase_17 AC 64 ms
10,740 KB
testcase_18 AC 63 ms
10,740 KB
testcase_19 AC 70 ms
10,484 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)

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

for _ in range(H):
    if is_zero == 1:
        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