結果

問題 No.2494 Sum within Components
ユーザー bortikbortik
提出日時 2023-10-06 22:02:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,509 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 211,684 KB
実行使用メモリ 16,404 KB
最終ジャッジ日時 2023-10-06 22:02:07
合計ジャッジ時間 3,903 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 8 ms
4,504 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 12 ms
4,380 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 101 ms
12,616 KB
testcase_15 AC 99 ms
11,176 KB
testcase_16 AC 40 ms
10,652 KB
testcase_17 AC 36 ms
13,152 KB
testcase_18 AC 42 ms
13,736 KB
testcase_19 AC 121 ms
16,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, n, a) for (int i = (int)(n); i >= (int)(a); i--)

inline ll positive_modulo(ll i, ll n) {
    return (i % n + n) % n;
}

const ll P = 998244353;

void solve(){
    int n, m; cin >> n >> m;
    vector<ll> A(n);
    stack<int> todo;
    rep(i,0,n) {
        cin >> A[i];
        todo.push(i);
    }
    vector<vector<int>> adj(n);
    rep(i,0,m){
        int x,y; cin >> x >> y;
        x--;y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    int last_color = 0;
    vector<ll> color_value;
    vector<int> color(n, -1);
    vector<bool> seen(n, false);
    while(!todo.empty()){
        int cur = todo.top();
        todo.pop();
        if(seen[cur]) continue;
        seen[cur] = true;
        if(color[cur] == -1){
            color[cur] = last_color;
            last_color++;
            color_value.push_back(0);
        }
        color_value[color[cur]] = positive_modulo(color_value[color[cur]]+A[cur], P);
        for(auto& v:adj[cur]){
            if(!seen[v]){
                color[v] = color[cur];
                todo.push(v);
            }
        }
    }

    ll answer = 1;
    rep(i,0,n){
        answer = positive_modulo(answer*color_value[color[i]], P);
    }

    cout << answer;
    
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    rep(i,0,t) solve();

    return 0;
}
0