結果

問題 No.2494 Sum within Components
ユーザー CecilCecil
提出日時 2023-10-06 23:28:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,711 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 11,112 KB
実行使用メモリ 27,768 KB
最終ジャッジ日時 2023-10-06 23:28:31
合計ジャッジ時間 5,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,748 KB
testcase_01 AC 20 ms
8,564 KB
testcase_02 AC 19 ms
8,704 KB
testcase_03 AC 19 ms
8,744 KB
testcase_04 AC 19 ms
8,644 KB
testcase_05 AC 19 ms
8,640 KB
testcase_06 AC 19 ms
8,744 KB
testcase_07 AC 18 ms
8,604 KB
testcase_08 AC 19 ms
8,600 KB
testcase_09 AC 82 ms
9,344 KB
testcase_10 AC 200 ms
11,652 KB
testcase_11 AC 41 ms
10,344 KB
testcase_12 AC 605 ms
11,172 KB
testcase_13 AC 315 ms
10,708 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from collections import defaultdict as dd

class UnionFind():
    # 初期化
    def __init__(self, n):
        self.par = [-1] * n
        self.rank = [0] * n
        self.siz = [1] * n

    # 根を求める
    def root(self, x):
        if self.par[x] == -1: return x # x が根の場合は x を返す
        else:
          self.par[x] = self.root(self.par[x]) # 経路圧縮
          return self.par[x]

    # x と y が同じグループに属するか (根が一致するか)
    def issame(self, x, y):
        return self.root(x) == self.root(y)

    # x を含むグループと y を含むグループを併合する
    def unite(self, x, y):
        # x 側と y 側の根を取得する
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry: return False # すでに同じグループのときは何もしない
        # union by rank
        if self.rank[rx] < self.rank[ry]: # ry 側の rank が小さくなるようにする
            rx, ry = ry, rx
        self.par[ry] = rx # ry を rx の子とする
        if self.rank[rx] == self.rank[ry]: # rx 側の rank を調整する
            self.rank[rx] += 1
        self.siz[rx] += self.siz[ry] # rx 側の siz を調整する
        return True
    
    # x を含む根付き木のサイズを求める
    def size(self, x):
        return self.siz[self.root(x)]

N,M = map(int, input().split())
A =list(map(int, input().split()))
G = UnionFind(N+1)
score = dd(lambda:0)
for _ in range(M):
    u,m = map(int, input().split())
    G.unite(u,m)
for i in range(1, N+1):
    score[G.root(i)] += A[i-1]
ans = 1
for i in range(1, N+1):
    ans *= score[G.root(i)] 
print(ans%998244353)
0