結果

問題 No.2291 Union Find Estimate
ユーザー prin_kemkemprin_kemkem
提出日時 2023-05-06 10:59:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 3,589 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,944 KB
実行使用メモリ 98,532 KB
最終ジャッジ日時 2024-05-03 00:31:42
合計ジャッジ時間 3,262 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
79,884 KB
testcase_01 AC 79 ms
80,352 KB
testcase_02 AC 176 ms
98,532 KB
testcase_03 AC 96 ms
83,872 KB
testcase_04 AC 81 ms
80,200 KB
testcase_05 AC 83 ms
80,184 KB
testcase_06 AC 81 ms
80,380 KB
testcase_07 AC 82 ms
79,940 KB
testcase_08 AC 93 ms
80,284 KB
testcase_09 AC 97 ms
80,836 KB
testcase_10 AC 122 ms
82,048 KB
testcase_11 AC 125 ms
82,968 KB
testcase_12 AC 134 ms
84,916 KB
testcase_13 AC 99 ms
81,072 KB
testcase_14 AC 106 ms
81,280 KB
testcase_15 AC 129 ms
83,304 KB
testcase_16 AC 129 ms
81,664 KB
testcase_17 AC 104 ms
80,860 KB
testcase_18 AC 109 ms
80,948 KB
testcase_19 AC 126 ms
82,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque, Counter
import copy
from itertools import combinations, permutations, product, accumulate, groupby
from heapq import heapify, heappop, heappush
import math
import bisect
from pprint import pprint
import sys
# sys.setrecursionlimit(700000)
input = lambda: sys.stdin.readline().rstrip('\n')
inf = float('inf')
mod1 = 10**9+7
mod2 = 998244353
def ceil_div(x, y): return -(-x//y)

#################################################

class UnionFind:
    #コンストラクタ
    def __init__(self, n):
        self.n = n
        self.parents = [-1]*n

    #点xの根を調べる+親が根になるよう移動
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    #点x,yの属する集合同士を連結(要素数が少ない方を多い方に連結)
    #辺を追加したらTrue, しなければFalseを返す
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if x > y:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        return True

    #点xが属する集合の要素数を取得
    def size(self, x):
        return -self.parents[self.find(x)]

    #点x,yが同じ集合に属しているか判定
    def same(self, x, y):
        return self.find(x) == self.find(y)

    #点xの属する集合の全要素を取得
    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 get_groups(self):
        groups = defaultdict(list)
        for x in range(self.n):
            groups[self.find(x)].append(x)
        return groups

    #print(インスタンス)で、全集合の「根と全要素」を出力
    def __str__(self):
        return '\n'.join('{}:{}'.format(r, self.menbers(r)) for r in self.roots())

def is_int(s):
    return ord("0") <= ord(s) <= ord("9")
def is_str(s):
    return ord("a") <= ord(s) <= ord("z")

W, H = map(int, input().split())
uf = UnionFind(W)
Q = [input() for _ in range(H)]
edge = 0
ok = 0
memo = [None]*W
ans = []
out = False
for h, q in enumerate(Q):
    d = {}
    for i, s in enumerate(q):
        if is_int(s):
            r = uf.find(i)
            if memo[r] is not None and memo[r] != s:
                out = True
                break
            elif memo[r] is None:
                memo[r] = s
                ok += 1
        elif is_str(s):
            if s in d:
                ri, rj = uf.find(i), d[s]
                if memo[ri] is not None and memo[rj] is not None:
                    if memo[ri] != memo[rj]:
                        out = True
                        break
                    else:
                        b = uf.union(ri, rj)
                        edge += b
                        ok -= b
                elif memo[ri] is not None:
                    memo[rj] = memo[ri]
                    edge += uf.union(ri, rj)
                else:
                    edge += uf.union(ri, rj)
            else:
                d[s] = uf.find(i)
    if out:
        ans.extend([0]*(H-h))
        break
    ans.append(pow(10, W-edge-ok, mod2))
print(*ans, sep="\n")
0