結果

問題 No.2494 Sum within Components
ユーザー shinchanshinchan
提出日時 2023-09-05 12:00:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,526 bytes
コンパイル時間 2,282 ms
コンパイル使用メモリ 206,916 KB
実行使用メモリ 12,704 KB
最終ジャッジ日時 2024-06-23 07:37:43
合計ジャッジ時間 8,396 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 1,127 ms
5,376 KB
testcase_11 AC 332 ms
5,376 KB
testcase_12 AC 195 ms
5,376 KB
testcase_13 AC 422 ms
5,376 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

struct UnionFind {
    vector<int> par;
    UnionFind(int n) :par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    bool connect(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    int size(int x) {
        return -par[root(x)];
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n, m;
    cin >> n >> m;
    
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    UnionFind uf(n);
    rep(i, m) {
        int x, y;
        cin >> x >> y;
        x --; y --;
        uf.connect(x, y);
    }
    ll ans = 1;
    vector<ll> num(n, -1);
    rep(i, n) {
        int r = uf.root(i);
        if(num[r] != -1) {
            ans *= num[r];
            ans %= MOD998;
        } else {
            ll s = 0;
            rep(j, n) if(uf.root(j) == r) s += a[j];
            s %= MOD998;
            num[r] = s;
            ans *= s;
            ans %= MOD998;
        }
    }
    cout << ans << endl;
    return 0;
}
0