結果

問題 No.2291 Union Find Estimate
ユーザー 👑 KazunKazun
提出日時 2023-05-05 21:49:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 4,653 bytes
コンパイル時間 380 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 93,372 KB
最終ジャッジ日時 2023-08-15 03:41:23
合計ジャッジ時間 4,947 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,652 KB
testcase_01 AC 95 ms
71,664 KB
testcase_02 AC 178 ms
80,992 KB
testcase_03 AC 204 ms
93,372 KB
testcase_04 AC 237 ms
78,920 KB
testcase_05 AC 241 ms
79,460 KB
testcase_06 AC 194 ms
78,792 KB
testcase_07 AC 167 ms
78,364 KB
testcase_08 AC 126 ms
78,624 KB
testcase_09 AC 127 ms
78,312 KB
testcase_10 AC 129 ms
78,032 KB
testcase_11 AC 132 ms
78,376 KB
testcase_12 AC 130 ms
78,736 KB
testcase_13 AC 128 ms
79,560 KB
testcase_14 AC 262 ms
79,284 KB
testcase_15 AC 238 ms
83,784 KB
testcase_16 AC 260 ms
80,756 KB
testcase_17 AC 145 ms
78,568 KB
testcase_18 AC 149 ms
78,860 KB
testcase_19 AC 131 ms
78,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Coloring_Union_Find():
    __slots__=("n", "parents", "rank", "data", "merge", "__group_number")

    def __init__(self, N, merge, unit):
        """ 0,1,...,N-1 を要素として初期化する.

        N: 要素数
        merge: 合成の方法
        e: 最初の値
        """
        self.n=N
        self.parents=[-1]*N
        self.data=[unit]*N
        self.rank=[0]*N
        self.merge=merge
        self.__group_number=N

    def find(self, x):
        """ 要素 x の属している族を調べる.

        x: 要素
        """
        V=[]
        while self.parents[x]>=0:
            V.append(x)
            x=self.parents[x]

        for v in V:
            self.parents[v]=x
        return x

    def union(self, x, y):
        """ 要素 x,y を同一視し, それぞれが持っている族の色を統合する.

        x,y: 要素
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            return

        self.__group_number-=1

        self.data[x]=self.data[y]=self.merge(self.data[x], self.data[y])

        if self.rank[x]<self.rank[y]:
            x,y=y,x

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

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1

    def size(self, x):
        """ 要素 x の属している族の要素の数.

        x: 要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """ 要素 x,y は同一視されているか?

        x,y: 要素
        """
        return self.find(x) == self.find(y)

    def update(self, x, color):
        """ 要素 x の属する族の色を color に変更する.

        x: 要素
        color: 色
        """
        self.data[self.find(x)]=color

    def get(self, x):
        """ 要素 x の属する属の色を求める.

        x: 要素
        """
        return self.data[self.find(x)]

    def members(self, x):
        """ 要素 x が属している族の要素.
        ※族の要素の個数が欲しいときは size を使うこと!!

        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 self.__group_number

    def all_group_members(self):
        """ 全ての族の出力
        """
        X={r:[] for r in self.roots()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def list(self):
        return [self.get(x) for x in range(self.n)]

    def map(self):
        return {x:self.get(x) for x in self.roots()}

    def __str__(self):
        string=[]
        for x,g in self.all_group_members().items():
            string.append(" ({}) {}".format(self.get(x), g))
        return ",".join(string)

    def __repr__(self):
        return "Coloring Union Find:"+str(self)

    def __getitem__(self,index):
        return self.data[self.find(index)]

    def __setitem__(self,index,value):
        self.data[self.find(index)]=value
#==================================================
def solve():
    from collections import defaultdict

    W,H=map(int,input().split())

    def merge(a,b):
        if a!=-1:
            return a
        else:
            return b

    U=Coloring_Union_Find(W, merge, -1)

    Mod=998244353
    TEN=[0]*(W+1); TEN[0]=1
    for j in range(1,W+1):
        TEN[j]=(10*TEN[j-1])%Mod

    flag=1
    Ans=[0]*H
    S=0
    for i in range(H):
        Q=input()
        data=defaultdict(list)
        for j in range(W):
            if "0"<=Q[j]<="9":
                x=int(Q[j])
                if U.get(j)!=-1 and U.get(j)!=x:
                    flag=False
                if U.get(j)==-1:
                    S+=1
                U.update(j,x)
            elif "a"<=Q[j]<="z":
                data[Q[j]].append(j)
            else:
                pass

        for a in data:
            for k in range(len(data[a])-1):
                p=U.get(data[a][k]); q=U.get(data[a][k+1])
                if p!=-1 and q!=-1:
                    if p!=q:
                        flag=False
                    if p==q and not U.same(data[a][k], data[a][k+1]):
                        S-=1
                U.union(data[a][k], data[a][k+1])

        Ans[i]=TEN[U.group_count()-S] if flag else 0

    return Ans


#==================================================
import sys
input=sys.stdin.readline
write=sys.stdout.write

write("\n".join(map(str,solve())))
0