結果

問題 No.2494 Sum within Components
ユーザー umezo
提出日時 2023-10-06 22:51:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,247 bytes
コンパイル時間 2,251 ms
コンパイル使用メモリ 201,384 KB
最終ジャッジ日時 2025-02-17 05:25:25
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

const int MOD=998244353;
class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return root(x)==root(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(root(x),root(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int root(int x){
    if(x != p[x]) p[x]=root(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[root(x)];
  }
};
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n,m;
  cin>>n>>m;
  vector<ll> A(n),B(n);
  rep(i,n) cin>>A[i];
  Dis ds=Dis(n);
  rep(i,m){
    int u,v;
    cin>>u>>v;
    u--,v--;
    ds.unite(u,v);
  }
  rep(i,n){
    B[ds.root(i)]=(B[ds.root(i)]+A[i])%MOD;
  }
  ll ans=1;
  rep(i,n){
    ans=ans*B[ds.root(i)]%MOD;
  }
  cout<<ans<<endl;
    
  return 0;
}
0