結果

問題 No.2494 Sum within Components
コンテスト
ユーザー KumaTachiRen
提出日時 2023-09-12 12:08:05
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 1,017 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 251 ms
コンパイル使用メモリ 85,168 KB
実行使用メモリ 108,460 KB
最終ジャッジ日時 2026-03-19 16:43:08
合計ジャッジ時間 3,365 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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