結果

問題 No.2291 Union Find Estimate
ユーザー chankei271828chankei271828
提出日時 2023-05-05 22:14:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,553 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 88,960 KB
最終ジャッジ日時 2024-05-02 16:41:46
合計ジャッジ時間 3,186 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 42 ms
52,736 KB
testcase_02 AC 265 ms
88,132 KB
testcase_03 AC 92 ms
88,960 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 157 ms
77,828 KB
testcase_11 AC 166 ms
77,916 KB
testcase_12 AC 203 ms
78,848 KB
testcase_13 AC 104 ms
77,824 KB
testcase_14 AC 108 ms
76,800 KB
testcase_15 AC 116 ms
81,024 KB
testcase_16 AC 106 ms
76,952 KB
testcase_17 AC 107 ms
77,164 KB
testcase_18 AC 104 ms
76,776 KB
testcase_19 AC 122 ms
76,696 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]==-1:
        A.append(-1)
        continue
    ans=1
    alpha=[[] for _ in range(26)]
    for i in range(W):
        if 48<=ord(res[i])<=57:
            t=int(res[i])
            if a[U.find(i)]!=-1 and a[U.find(i)]!=t:
                ans=-1
                break
            elif a[U.find(i)]==-1:
                a[U.find(i)]=t
        if 97<=ord(res[i])<=97+25:
            alpha[ord(res[i])-97].append(i)
    if ans==-1:
        A.append(ans)
        continue
    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=-1
                    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==-1:
                break
    if ans==-1:
        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