結果

問題 No.2494 Sum within Components
ユーザー KumaTachiRenKumaTachiRen
提出日時 2023-09-12 12:08:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 403 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 106,092 KB
最終ジャッジ日時 2023-09-12 12:08:11
合計ジャッジ時間 5,866 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,304 KB
testcase_01 AC 77 ms
71,136 KB
testcase_02 AC 77 ms
71,328 KB
testcase_03 AC 77 ms
70,868 KB
testcase_04 AC 77 ms
71,152 KB
testcase_05 AC 76 ms
71,388 KB
testcase_06 AC 77 ms
71,232 KB
testcase_07 AC 79 ms
71,148 KB
testcase_08 AC 77 ms
71,328 KB
testcase_09 AC 157 ms
77,968 KB
testcase_10 AC 181 ms
79,828 KB
testcase_11 AC 137 ms
78,116 KB
testcase_12 AC 204 ms
80,268 KB
testcase_13 AC 184 ms
79,928 KB
testcase_14 AC 389 ms
95,748 KB
testcase_15 AC 335 ms
86,872 KB
testcase_16 AC 207 ms
93,976 KB
testcase_17 AC 131 ms
102,580 KB
testcase_18 AC 190 ms
105,748 KB
testcase_19 AC 403 ms
106,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

class UnionFind:
    def __init__(self, n, data):
        self.n = n
        self.par = [-1] * n
        self.d = list(data)

    def find(self, x):
        if self.par[x] < 0: return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y: return
        if self.par[x] > self.par[y]: x, y = y, x
        self.par[x] += self.par[y]
        self.par[y] = x
        self.d[x] += self.d[y]
        if self.d[x] >= MOD: self.d[x] -= MOD

    def size(self, x): return -self.par[self.find(x)]

    def data(self, x): return self.d[self.find(x)]


n, m = map(int, input().split(" "))
a = list(map(int, input().split(" ")))

uf = UnionFind(n, a)

for _ in range(m):
	u, v = map(int, input().split(" "))
	uf.unite(u - 1, v - 1)

ans = 1
for x in range(n):
	if uf.find(x) != x: continue
	c = uf.size(x)
	s = uf.data(x)
	for _ in range(c):
		ans = ans * s % MOD

print(ans)
0