結果
問題 | No.2494 Sum within Components |
ユーザー |
|
提出日時 | 2023-10-06 21:37:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 412 ms / 2,000 ms |
コード長 | 1,471 bytes |
コンパイル時間 | 203 ms |
コンパイル使用メモリ | 82,448 KB |
実行使用メモリ | 152,316 KB |
最終ジャッジ日時 | 2024-07-26 15:52:01 |
合計ジャッジ時間 | 3,899 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
import syssys.setrecursionlimit(5*10**5)input = sys.stdin.readlinefrom collections import defaultdict, deque, Counterfrom heapq import heappop, heappushfrom bisect import bisect_left, bisect_rightfrom math import gcdfrom collections import defaultdictclass UnionFind:def __init__(self, n):self.n = nself.p = [-1] * (n+1)def find(self, x):if self.p[x] < 0:return xelse:self.p[x] = self.find(self.p[x])return self.p[x]def union(self, x, y):x = self.find(x)y = self.find(y)if x == y:returnif self.p[x] > self.p[y]:x, y = y, xself.p[x] += self.p[y]self.p[y] = xdef same(self, a, b):return self.find(a) == self.find(b)def group(self):d = defaultdict(list)for i in range(1, self.n+1):par = self.find(i)d[par].append(i)return dmod = 998244353n,m = map(int,input().split())A = list(map(int,input().split()))uf = UnionFind(n)graph = [[] for i in range(n+1)]for i in range(m):a,b = map(int,input().split())graph[a].append(b)graph[b].append(a)uf.union(a,b)g = uf.group()tot = [0]*(n+1)for k,v in g.items():tmp = 0for i in v:tmp += A[i-1]tmp %= modfor i in v:tot[i] = tmpans = 1for i in range(1, n+1):ans *= tot[i]ans %= modprint(ans)