結果

問題 No.2494 Sum within Components
ユーザー shinchan
提出日時 2023-09-05 12:00:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,526 bytes
コンパイル時間 1,982 ms
コンパイル使用メモリ 199,616 KB
最終ジャッジ日時 2025-02-16 18:45:18
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

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