結果

問題 No.2291 Union Find Estimate
ユーザー chankei271828chankei271828
提出日時 2023-05-05 22:34:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 2,523 bytes
コンパイル時間 672 ms
コンパイル使用メモリ 86,772 KB
実行使用メモリ 96,880 KB
最終ジャッジ日時 2023-08-15 05:17:01
合計ジャッジ時間 4,620 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,068 KB
testcase_01 AC 75 ms
71,096 KB
testcase_02 AC 280 ms
89,608 KB
testcase_03 AC 118 ms
96,880 KB
testcase_04 AC 87 ms
75,148 KB
testcase_05 AC 88 ms
75,684 KB
testcase_06 AC 84 ms
75,400 KB
testcase_07 AC 82 ms
75,188 KB
testcase_08 AC 106 ms
76,352 KB
testcase_09 AC 122 ms
76,948 KB
testcase_10 AC 178 ms
78,556 KB
testcase_11 AC 186 ms
78,896 KB
testcase_12 AC 224 ms
79,776 KB
testcase_13 AC 120 ms
78,532 KB
testcase_14 AC 128 ms
77,316 KB
testcase_15 AC 139 ms
82,864 KB
testcase_16 AC 128 ms
77,808 KB
testcase_17 AC 123 ms
77,780 KB
testcase_18 AC 123 ms
77,856 KB
testcase_19 AC 139 ms
78,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind:#根っこ参照で付け直し、デカイ木にちっさい木をつける
    def __init__(self,n):
        self.parent=[i for i in range(n)]#自分の親頂点の番号
        self.num=[1]*n#自分を根とする部分木の頂点の個数
        self.rank=[1]*n#自分を根とする部分木の高さ
    def find(self,i):
        if self.parent[i]==i:
            return i
        else:
            self.parent[i]=self.find(self.parent[i])
            return self.parent[i]
    def union(self,i,j):
        a=self.find(i)
        b=self.find(j)
        if self.rank[a]>self.rank[b]:
            self.parent[b]=a
            
            self.num[a]+=self.num[b]
        elif self.rank[a]<self.rank[b]:
            self.parent[a]=b
            self.num[b]+=self.num[a]
        else:
            if a!=b:
                self.rank[a]+=1
                self.parent[b]=a
                self.num[a]+=self.num[b]
    def numb(self,i):#その頂点を含む連結成分の点の個数
        j=self.find(i)
        return self.num[j]
    
W,H=map(int,input().split())
a=[-1]*W
U=Unionfind(W)
mod=998244353
A=[]
for _ in range(H):
    res=input()
    if A and A[-1]==0:
        A.append(0)
        continue
    ans=1
    alpha=[[] for _ in range(26)]
    num=[]
    for i in range(W):
        if 48<=ord(res[i])<=57:
            num.append(i)
        if 97<=ord(res[i])<=97+25:
            alpha[ord(res[i])-97].append(i)
    for i in range(26):
        l=len(alpha[i])
        if l!=0:
            for j in range(l-1):
                if a[U.find(alpha[i][j])]!=-1 and a[U.find(alpha[i][j+1])]!=-1 and a[U.find(alpha[i][j])]!=a[U.find(alpha[i][j+1])]:
                    ans=0
                    break
                aa=U.find(alpha[i][j])
                bb=U.find(alpha[i][j+1])
                if aa!=bb:
                    U.union(alpha[i][j],alpha[i][j+1])
                    if a[aa]!=-1 and a[bb]==-1:
                        a[U.find(alpha[i][j])]=a[aa]
                    if a[aa]==-1 and a[bb]!=-1:
                        a[U.find(alpha[i][j])]=a[bb]
            if ans==0:
                break
    for i in num:
        t=int(res[i])
        if a[U.find(i)]!=-1 and a[U.find(i)]!=t:
            ans=0
            break
        elif a[U.find(i)]==-1:
            a[U.find(i)]=t
    if ans==0:
        A.append(ans)
        continue
    for i in range(W):
        if U.find(i)==i and a[U.find(i)]==-1:
            ans*=10
            ans%=mod
    A.append(ans)
for t in A:
    print(t)
0