結果

問題 No.2494 Sum within Components
ユーザー Akijin_007Akijin_007
提出日時 2023-10-06 22:23:45
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,688 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 87,260 KB
実行使用メモリ 103,344 KB
最終ジャッジ日時 2023-10-06 22:23:58
合計ジャッジ時間 10,325 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,164 KB
testcase_01 AC 69 ms
71,204 KB
testcase_02 AC 68 ms
71,164 KB
testcase_03 AC 70 ms
71,668 KB
testcase_04 AC 71 ms
71,632 KB
testcase_05 AC 70 ms
71,484 KB
testcase_06 AC 68 ms
71,368 KB
testcase_07 AC 73 ms
71,168 KB
testcase_08 AC 70 ms
71,288 KB
testcase_09 AC 139 ms
78,196 KB
testcase_10 TLE -
testcase_11 AC 567 ms
79,472 KB
testcase_12 AC 486 ms
80,332 KB
testcase_13 AC 1,013 ms
79,688 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#int(input())
#map(int, input().split())
#list(map(int, input().split()))

import sys
sys.setrecursionlimit(200000)

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

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

N, M = map(int, input().split())
A = list(map(int, input().split()))

u = UnionFind(N)
for i in range(M):
    a, b = map(int, input().split())
    u.union(a-1, b-1)

mod = 998244353

# p = [0] * N
m = u.all_group_members()
ans = 1

for k, v in m.items():
    s = 0
    for x in v:
        s += A[x]
        s %= mod
    # print(k, v)
    # print(pow(s, len(v), mod))
    ans = (ans * pow(s, len(v), mod)) % mod

print(ans)

0