結果
問題 | No.2494 Sum within Components |
ユーザー |
![]() |
提出日時 | 2023-10-07 10:39:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 382 ms / 2,000 ms |
コード長 | 701 bytes |
コンパイル時間 | 390 ms |
コンパイル使用メモリ | 82,352 KB |
実行使用メモリ | 132,204 KB |
最終ジャッジ日時 | 2024-07-26 17:45:22 |
合計ジャッジ時間 | 3,912 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
def root(x):if P[x] < 0: return xP[x] = root(P[x]) # 経路圧縮return P[x]def unite(x,y):x = root(x)y = root(y)if x == y: returnif x > y: x,y = y,xP[x] += P[y]P[y] = xdef same(x,y):return root(x) == root(y)def size(x):x = root(x)return -P[x]MOD = 998244353N,M = map(int,input().split())A = list(map(int,input().split()))UV = [list(map(int,input().split())) for _ in range(M)]P = [-1] * Nfor u,v in UV:unite(u-1,v-1)from collections import defaultdictdic = defaultdict(int)for i,a in enumerate(A):dic[root(i)] += adic[root(i)] %= MODans = 1for i in range(N):ans *= dic[root(i)]ans %= MODprint(ans)