結果

問題 No.2494 Sum within Components
ユーザー mymelochanmymelochan
提出日時 2023-10-06 21:38:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 317 ms / 2,000 ms
コード長 1,364 bytes
コンパイル時間 428 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 113,176 KB
最終ジャッジ日時 2023-10-06 21:38:33
合計ジャッジ時間 4,701 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,436 KB
testcase_01 AC 91 ms
71,784 KB
testcase_02 AC 92 ms
71,584 KB
testcase_03 AC 115 ms
71,836 KB
testcase_04 AC 90 ms
71,724 KB
testcase_05 AC 88 ms
71,904 KB
testcase_06 AC 90 ms
71,668 KB
testcase_07 AC 90 ms
71,752 KB
testcase_08 AC 88 ms
71,636 KB
testcase_09 AC 140 ms
78,688 KB
testcase_10 AC 151 ms
80,188 KB
testcase_11 AC 117 ms
78,036 KB
testcase_12 AC 186 ms
79,788 KB
testcase_13 AC 158 ms
79,196 KB
testcase_14 AC 288 ms
101,272 KB
testcase_15 AC 280 ms
93,968 KB
testcase_16 AC 179 ms
99,176 KB
testcase_17 AC 154 ms
113,176 KB
testcase_18 AC 173 ms
111,612 KB
testcase_19 AC 317 ms
111,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import sys
sys.setrecursionlimit(10**7)

from heapq import heappop,heappush
from collections import deque,defaultdict,Counter
from bisect import bisect_left, bisect_right
from itertools import product,combinations,permutations

ipt = sys.stdin.readline

def iin():
    return int(ipt())
def lmin():
    return list(map(int,ipt().split()))

class dsu:

    def __init__(self,n):
        self.N = n
        self.cnt=[1]*self.N
        self.root=list(range(self.N))
        self.components = self.N

    def unite(self,x,y):
        x=self.leader(x)
        y=self.leader(y)
        if x!=y:
            self.components -= 1
            if self.cnt[x]<self.cnt[y]:
                x,y=y,x
            self.cnt[x]+=self.cnt[y]
            self.root[y]=x
            return x
        return None

    def leader(self,x):
        if self.root[x]==x:
            return x
        self.root[x]=self.leader(self.root[x])
        return self.root[x]

MOD = 998244353
#############################################################

N,M = lmin()
A = lmin()
uf = dsu(N)
for _ in range(M):
    u,v = lmin()
    u,v = u-1,v-1
    uf.unite(u,v)

cnt = [0]*N
for i in range(N):
    l = uf.leader(i)
    cnt[l] += A[i]
    cnt[l] %= MOD

ans = 1
for i in range(N):
    ans *= cnt[uf.leader(i)]
    ans %= MOD

print(ans)
0