結果

問題 No.2291 Union Find Estimate
ユーザー lloyzlloyz
提出日時 2023-05-05 21:59:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 550 ms / 2,000 ms
コード長 3,559 bytes
コンパイル時間 460 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 179,980 KB
最終ジャッジ日時 2023-08-15 04:01:20
合計ジャッジ時間 5,524 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,512 KB
testcase_01 AC 93 ms
71,508 KB
testcase_02 AC 550 ms
77,788 KB
testcase_03 AC 281 ms
179,980 KB
testcase_04 AC 105 ms
76,548 KB
testcase_05 AC 104 ms
76,576 KB
testcase_06 AC 102 ms
76,300 KB
testcase_07 AC 103 ms
76,624 KB
testcase_08 AC 139 ms
78,068 KB
testcase_09 AC 150 ms
78,584 KB
testcase_10 AC 254 ms
80,224 KB
testcase_11 AC 248 ms
78,668 KB
testcase_12 AC 317 ms
80,040 KB
testcase_13 AC 183 ms
94,168 KB
testcase_14 AC 160 ms
78,600 KB
testcase_15 AC 287 ms
129,460 KB
testcase_16 AC 171 ms
79,836 KB
testcase_17 AC 181 ms
81,872 KB
testcase_18 AC 176 ms
81,456 KB
testcase_19 AC 198 ms
78,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素xを含む集合の親を見つける関数。
        '''
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        要素xを含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素yを含む集合に統合される。
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        '''
        要素xを含む集合の要素数を出す関数。
        '''
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

w, h = map(int, input().split())
mod = 998244353

D = [-1 for _ in range(w)]
UF = UnionFind(w)
G = [set([i]) for i in range(w)]
roots = set(list(range(w)))
zero = False
for i in range(h):
    q = input()
    alph_set = defaultdict(list)
    for j in range(w):
        if q[j].isdigit():
            if D[j] == -1:
                D[j] = int(q[j])
            else:
                if D[j] != int(q[j]):
                    zero = True
                    break
        elif q[j].isalpha():
            alph_set[q[j]].append(j)
    if zero:
        for j in range(i, h):
            print(0)
        break
    for L in alph_set.values():
        if len(L) == 1:
            continue
        f = L[0]
        for j in range(1, len(L)):
            if not UF.same(f, L[j]):
                froot = UF.find(f)
                lroot = UF.find(L[j])
                UF.union(f, L[j])
                root = UF.find(f)
                if root == froot:
                    roots.remove(lroot)
                    G[froot] |= G[lroot]
                    G[lroot] = None
                else:
                    roots.remove(froot)
                    G[lroot] |= G[froot]
                    G[froot] = None
    for j in range(w):
        if G[j] is None:
            continue
        num_set = set()
        for idx in G[j]:
            if D[idx] == -1:
                continue
            num_set.add(D[idx])
        if len(num_set) > 1:
            zero = True
            break
        for num in num_set:
            for idx in G[j]:
                D[idx] = num
    if zero:
        for j in range(i, h):
            print(0)
        break
    ans = 1
    for root in roots:
        if D[root] == -1:
            ans *= 10
            ans %= mod
    print(ans)
0