結果

問題 No.2494 Sum within Components
ユーザー rlangevinrlangevin
提出日時 2023-10-06 21:34:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 335 ms / 2,000 ms
コード長 1,218 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 87,232 KB
実行使用メモリ 107,636 KB
最終ジャッジ日時 2023-10-06 21:34:39
合計ジャッジ時間 4,493 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,264 KB
testcase_01 AC 75 ms
71,580 KB
testcase_02 AC 73 ms
71,296 KB
testcase_03 AC 74 ms
71,416 KB
testcase_04 AC 73 ms
71,352 KB
testcase_05 AC 73 ms
71,496 KB
testcase_06 AC 74 ms
71,348 KB
testcase_07 AC 72 ms
71,576 KB
testcase_08 AC 73 ms
71,400 KB
testcase_09 AC 126 ms
78,072 KB
testcase_10 AC 149 ms
79,708 KB
testcase_11 AC 102 ms
77,888 KB
testcase_12 AC 175 ms
79,932 KB
testcase_13 AC 160 ms
79,296 KB
testcase_14 AC 295 ms
100,240 KB
testcase_15 AC 266 ms
93,264 KB
testcase_16 AC 194 ms
97,648 KB
testcase_17 AC 139 ms
106,544 KB
testcase_18 AC 167 ms
107,636 KB
testcase_19 AC 335 ms
106,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

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

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


N, M = map(int, input().split())
A = list(map(int, input().split()))
U = UnionFind(N)
for i in range(M):
    u, v = map(int, input().split())
    u, v = u - 1, v - 1
    U.union(u, v)
    
ans = [0] * N
for i in range(N):
    ans[U.find(i)] += A[i]
    
ans1 = 1
mod = 998244353
for i in range(N):
    ans1 *= ans[U.find(i)]
    ans1 %= mod
    
print(ans1)
0