結果

問題 No.2494 Sum within Components
ユーザー matt rohmatt roh
提出日時 2023-10-06 21:53:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 850 bytes
コンパイル時間 2,927 ms
コンパイル使用メモリ 249,728 KB
実行使用メモリ 17,096 KB
最終ジャッジ日時 2023-10-06 21:53:12
合計ジャッジ時間 4,640 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 13 ms
4,596 KB
testcase_13 AC 8 ms
4,388 KB
testcase_14 AC 101 ms
14,420 KB
testcase_15 AC 103 ms
14,264 KB
testcase_16 AC 38 ms
9,704 KB
testcase_17 AC 29 ms
6,320 KB
testcase_18 AC 40 ms
11,476 KB
testcase_19 AC 120 ms
17,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll md=998244353;
vector<ll>adj[202020];

ll pw(ll a,ll b)
{
    ll c=1;
    while(b>0)
    {
        if(b&1)c=c*a%md;
        a=a*a%md;
        b>>=1;
    }
    return c;
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    ll n,m;cin>>n>>m;
    vector<ll>vec(n);for(ll&i:vec)cin>>i;
    for(int i=0;i<m;i++)
    {
        ll u,v;cin>>u>>v;
        u--;v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    vector<ll>vis(n);
    auto dfs=[&](auto dfs,ll v,ll&val,ll&sz)->void
    {
        if(vis[v])return;
        val=(val+vec[v])%md;sz++;vis[v]=1;
        for(ll u:adj[v])dfs(dfs,u,val,sz);
    };
    ll ans=1;
    for(int i=0;i<n;i++)
    {
        ll val=0,sz=0;
        dfs(dfs,i,val,sz);
        if(sz>0)ans=ans*pw(val,sz)%md;
    }
    cout<<ans;
}
0