結果

問題 No.2494 Sum within Components
ユーザー GOTKAKOGOTKAKO
提出日時 2023-10-06 21:49:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,067 bytes
コンパイル時間 1,778 ms
コンパイル使用メモリ 175,944 KB
実行使用メモリ 14,812 KB
最終ジャッジ日時 2023-10-06 21:49:56
合計ジャッジ時間 3,382 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 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 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 4 ms
4,384 KB
testcase_12 AC 11 ms
4,432 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 95 ms
12,352 KB
testcase_15 AC 101 ms
12,096 KB
testcase_16 AC 41 ms
8,776 KB
testcase_17 AC 40 ms
9,188 KB
testcase_18 AC 46 ms
9,908 KB
testcase_19 AC 114 ms
14,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M; cin >> N >> M;
    vector<long long> A(N);
    for(auto &a : A) cin >> a;
    long long mod = 998244353;

    vector<vector<long long>> Graph(N);
    for(int i=0; i<M; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        Graph.at(u).push_back(v);
        Graph.at(v).push_back(u);
    }

    vector<bool> visited(N);
    long long answer = 1;  
    for(int i=0; i<N; i++){
        if(visited.at(i)) continue;
        visited.at(i) = true;

        long long now = 0,siz = 0;
        queue<int> Q; Q.push(i);
        while(Q.size()){
            int pos = Q.front(); Q.pop();
            now += A.at(pos); siz++;
            for(auto to : Graph.at(pos)){
                if(visited.at(to)) continue;
                visited.at(to) = true;
                Q.push(to);
            }
        }
        
        now %= mod;
        for(int k=0; k<siz; k++) answer = answer*now %mod;
        
    }
    cout << answer << endl;
}
0