結果

問題 No.2494 Sum within Components
ユーザー nikoro256nikoro256
提出日時 2023-10-06 22:05:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 1,645 bytes
コンパイル時間 1,266 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 146,476 KB
最終ジャッジ日時 2023-10-06 22:05:17
合計ジャッジ時間 5,512 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,876 KB
testcase_01 AC 90 ms
71,640 KB
testcase_02 AC 89 ms
71,760 KB
testcase_03 AC 90 ms
71,708 KB
testcase_04 AC 88 ms
71,652 KB
testcase_05 AC 94 ms
71,768 KB
testcase_06 AC 90 ms
71,800 KB
testcase_07 AC 91 ms
71,884 KB
testcase_08 AC 90 ms
71,408 KB
testcase_09 AC 169 ms
79,336 KB
testcase_10 AC 171 ms
80,284 KB
testcase_11 AC 141 ms
79,192 KB
testcase_12 AC 202 ms
80,744 KB
testcase_13 AC 187 ms
80,428 KB
testcase_14 AC 327 ms
101,892 KB
testcase_15 AC 314 ms
93,808 KB
testcase_16 AC 208 ms
99,308 KB
testcase_17 AC 196 ms
146,476 KB
testcase_18 AC 215 ms
119,612 KB
testcase_19 AC 361 ms
111,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

from collections import defaultdict
N,M=map(int,input().split())
A=list(map(int,input().split()))
uf=UnionFind(N)
dic=defaultdict(int)
for _ in range(M):
    u,v=map(int,input().split())
    u-=1
    v-=1
    uf.union(u,v)
p=998244353
ans=1
for i in range(N):
    dic[uf.find(i)]+=A[i]
for i in range(N):
    ans*=dic[uf.find(i)]
    ans%=p
print(ans)
0