結果
問題 | No.2494 Sum within Components |
ユーザー |
![]() |
提出日時 | 2023-10-06 22:02:03 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 138 ms / 2,000 ms |
コード長 | 1,509 bytes |
コンパイル時間 | 2,427 ms |
コンパイル使用メモリ | 206,188 KB |
最終ジャッジ日時 | 2025-02-17 05:03:34 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
#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;}