結果

問題 No.2494 Sum within Components
ユーザー flygonflygon
提出日時 2023-10-06 21:37:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 430 ms / 2,000 ms
コード長 1,471 bytes
コンパイル時間 255 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 128,048 KB
最終ジャッジ日時 2023-10-06 21:37:44
合計ジャッジ時間 5,039 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,764 KB
testcase_01 AC 93 ms
71,732 KB
testcase_02 AC 89 ms
71,972 KB
testcase_03 AC 90 ms
71,984 KB
testcase_04 AC 88 ms
71,924 KB
testcase_05 AC 89 ms
71,780 KB
testcase_06 AC 90 ms
71,632 KB
testcase_07 AC 90 ms
71,636 KB
testcase_08 AC 90 ms
71,632 KB
testcase_09 AC 145 ms
79,280 KB
testcase_10 AC 166 ms
81,040 KB
testcase_11 AC 123 ms
78,416 KB
testcase_12 AC 202 ms
81,164 KB
testcase_13 AC 174 ms
81,624 KB
testcase_14 AC 362 ms
107,488 KB
testcase_15 AC 348 ms
101,492 KB
testcase_16 AC 272 ms
114,956 KB
testcase_17 AC 183 ms
124,320 KB
testcase_18 AC 236 ms
128,048 KB
testcase_19 AC 430 ms
121,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd

from collections import defaultdict


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

    def find(self, x):
        if self.p[x] < 0:
            return x
        else:
            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:
            return
        if self.p[x] > self.p[y]:
            x, y = y, x
        self.p[x] += self.p[y]
        self.p[y] = x

    def 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 d

mod = 998244353
n,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 = 0
    for i in v:
        tmp += A[i-1]
        tmp %= mod
    for i in v:
        tot[i] = tmp
    
ans = 1
for i in range(1, n+1):
    ans *= tot[i]
    ans %= mod

print(ans)
0