結果

問題 No.2494 Sum within Components
ユーザー eve__fuyukieve__fuyuki
提出日時 2023-11-24 20:52:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 895 bytes
コンパイル時間 2,381 ms
コンパイル使用メモリ 214,208 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-24 20:52:28
合計ジャッジ時間 4,851 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 6 ms
6,676 KB
testcase_10 AC 6 ms
6,676 KB
testcase_11 AC 4 ms
6,676 KB
testcase_12 AC 8 ms
6,676 KB
testcase_13 AC 6 ms
6,676 KB
testcase_14 AC 53 ms
6,676 KB
testcase_15 AC 53 ms
6,676 KB
testcase_16 AC 26 ms
6,676 KB
testcase_17 AC 29 ms
6,676 KB
testcase_18 AC 31 ms
6,676 KB
testcase_19 AC 63 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/dsu>
#include <atcoder/modint>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
void fast_io() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

int main() {
    fast_io();
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for (auto &x : a) cin >> x;
    dsu uf(n);
    vector<mint> su(n);
    for (int i = 0; i < n; i++) su[i] = a[i];
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        if (uf.same(u, v)) {
            continue;
        }
        mint x = su[uf.leader(u)];
        mint y = su[uf.leader(v)];
        uf.merge(u, v);
        su[uf.leader(u)] = x + y;
    }
    mint ans = 1;
    for (int i = 0; i < n; i++) {
        if (uf.leader(i) == i) {
            ans *= su[i].pow(uf.size(i));
        }
    }
    cout << ans.val() << endl;
}
0