結果

問題 No.2494 Sum within Components
ユーザー hirayuu_ychirayuu_yc
提出日時 2023-10-06 21:30:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 1,078 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 87,348 KB
実行使用メモリ 121,772 KB
最終ジャッジ日時 2023-10-06 21:30:15
合計ジャッジ時間 4,708 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,444 KB
testcase_01 AC 68 ms
71,384 KB
testcase_02 AC 69 ms
71,444 KB
testcase_03 AC 68 ms
71,452 KB
testcase_04 AC 68 ms
71,560 KB
testcase_05 AC 70 ms
71,624 KB
testcase_06 AC 68 ms
71,380 KB
testcase_07 AC 68 ms
71,460 KB
testcase_08 AC 70 ms
71,524 KB
testcase_09 AC 146 ms
79,756 KB
testcase_10 AC 169 ms
81,912 KB
testcase_11 AC 118 ms
79,712 KB
testcase_12 AC 185 ms
81,540 KB
testcase_13 AC 185 ms
81,080 KB
testcase_14 AC 420 ms
101,420 KB
testcase_15 AC 422 ms
97,080 KB
testcase_16 AC 219 ms
99,636 KB
testcase_17 AC 130 ms
101,992 KB
testcase_18 AC 211 ms
121,772 KB
testcase_19 AC 489 ms
121,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self,size):
        self.uf=[[-1,0,A[i]] for i in range(size)]
        
    def unite(self,fir,sec):
        one=self.root(fir)
        two=self.root(sec)
        if one!=two:
            if self.uf[one][1]>self.uf[two][1]:
                one,two=two,one
            self.uf[one][0]=two
            self.uf[two][2]+=self.uf[one][2]
            if self.uf[one][1]==self.uf[two][1]:
                self.uf[two][1]+=1
    
    def same(self,fir,sec):
        return self.root(fir)==self.root(sec)
    
    def root(self,node):
        pos=node
        change=[]
        while self.uf[pos][0]!=-1:
            change.append(pos)
            pos=self.uf[pos][0]
        for i in change:
            self.uf[i][0]=pos
        return pos
    
    def size(self,node):
        return self.uf[self.root(node)][2]

N,M=map(int,input().split())
A=list(map(int,input().split()))
union=UnionFind(N)
for i in range(M):
    A,B=map(int,input().split())
    union.unite(A-1,B-1)
ans=1
for i in range(N):
    ans*=union.size(i)
    ans%=998244353
print(ans)
0