結果

問題 No.2494 Sum within Components
ユーザー miya145592miya145592
提出日時 2023-10-06 23:01:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 433 ms / 2,000 ms
コード長 1,662 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 87,208 KB
実行使用メモリ 134,868 KB
最終ジャッジ日時 2023-10-06 23:01:39
合計ジャッジ時間 5,286 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
71,664 KB
testcase_01 AC 96 ms
71,816 KB
testcase_02 AC 95 ms
71,820 KB
testcase_03 AC 95 ms
71,792 KB
testcase_04 AC 96 ms
71,928 KB
testcase_05 AC 96 ms
71,980 KB
testcase_06 AC 99 ms
71,616 KB
testcase_07 AC 97 ms
71,480 KB
testcase_08 AC 97 ms
71,700 KB
testcase_09 AC 160 ms
79,132 KB
testcase_10 AC 188 ms
82,660 KB
testcase_11 AC 134 ms
80,328 KB
testcase_12 AC 214 ms
80,460 KB
testcase_13 AC 195 ms
79,944 KB
testcase_14 AC 341 ms
102,108 KB
testcase_15 AC 323 ms
95,052 KB
testcase_16 AC 246 ms
100,264 KB
testcase_17 AC 195 ms
123,184 KB
testcase_18 AC 250 ms
134,868 KB
testcase_19 AC 433 ms
111,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n, w=None):
        self.par = [-1]*n
        self.rank = [0]*n
        self.siz = [1]*n
        self.cnt = n
        self.min_node = [i for i in range(n)]
        self.weight = w if w else [1]*n

    def root(self, x):
        if self.par[x] == -1:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def issame(self, x, y):
        return self.root(x) == self.root(y)
            
    def unite(self, x, y):
        px = self.root(x)
        py = self.root(y)
        if px == py:
            return False
        if self.rank[px] < self.rank[py]:
            px, py = py, px
        self.par[py] = px
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1
        self.siz[px] += self.siz[py]
        self.cnt -= 1
        self.min_node[px] = min(self.min_node[px], self.min_node[py])
        self.weight[px] += self.weight[py]
        return False

    def count(self):
        return self.cnt

    def min(self, x):
        return self.min_node[self.root(x)]

    def getweight(self, x):
        return self.weight[self.root(x)]
    
    def size(self, x):
        return self.siz[self.root(x)]
    
import sys
input = sys.stdin.readline
MOD = 998244353
N, M = map(int, input().split())
A = list(map(int, input().split()))
UF = UnionFind(N)
for _ in range(M):
    u, v = map(int, input().split())
    u-=1
    v-=1
    UF.unite(u, v)

from collections import defaultdict
D = defaultdict(int)
for i in range(N):
    r = UF.root(i)
    D[r] += A[i]
    D[r] %= MOD

ans = 1
for i in range(N):
    r = UF.root(i)
    ans *= D[r]
    ans %= MOD

print(ans)
0