結果

問題 No.2494 Sum within Components
ユーザー ntudantuda
提出日時 2023-10-07 10:39:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 408 ms / 2,000 ms
コード長 701 bytes
コンパイル時間 450 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 124,508 KB
最終ジャッジ日時 2023-10-07 10:39:10
合計ジャッジ時間 5,674 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,784 KB
testcase_01 AC 93 ms
71,852 KB
testcase_02 AC 95 ms
71,452 KB
testcase_03 AC 90 ms
71,596 KB
testcase_04 AC 92 ms
71,800 KB
testcase_05 AC 94 ms
71,788 KB
testcase_06 AC 93 ms
71,660 KB
testcase_07 AC 96 ms
71,448 KB
testcase_08 AC 93 ms
71,700 KB
testcase_09 AC 171 ms
81,140 KB
testcase_10 AC 178 ms
80,540 KB
testcase_11 AC 144 ms
79,100 KB
testcase_12 AC 205 ms
81,560 KB
testcase_13 AC 188 ms
80,368 KB
testcase_14 AC 384 ms
99,336 KB
testcase_15 AC 380 ms
101,600 KB
testcase_16 AC 230 ms
99,048 KB
testcase_17 AC 182 ms
118,572 KB
testcase_18 AC 239 ms
124,508 KB
testcase_19 AC 408 ms
116,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def root(x):
    if P[x] < 0: return x
    P[x] = root(P[x])  # 経路圧縮
    return P[x]
def unite(x,y):
    x = root(x)
    y = root(y)
    if x == y: return
    if x > y: x,y = y,x
    P[x] += P[y]
    P[y] = x

def same(x,y):
    return root(x) == root(y)

def size(x):
    x = root(x)
    return -P[x]
MOD = 998244353
N,M = map(int,input().split())
A = list(map(int,input().split()))
UV = [list(map(int,input().split())) for _ in range(M)]
P = [-1] * N
for u,v in UV:
    unite(u-1,v-1)
from collections import defaultdict
dic = defaultdict(int)
for i,a in enumerate(A):
    dic[root(i)] += a
    dic[root(i)] %= MOD
ans = 1
for i in range(N):
    ans *= dic[root(i)]
    ans %= MOD
print(ans)
0