結果

問題 No.2494 Sum within Components
ユーザー zezerozezero
提出日時 2023-11-04 13:59:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 932 bytes
コンパイル時間 3,515 ms
コンパイル使用メモリ 170,676 KB
実行使用メモリ 4,968 KB
最終ジャッジ日時 2023-11-04 14:00:03
合計ジャッジ時間 6,497 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 12 ms
4,348 KB
testcase_10 AC 13 ms
4,348 KB
testcase_11 AC 6 ms
4,348 KB
testcase_12 AC 20 ms
4,348 KB
testcase_13 AC 13 ms
4,348 KB
testcase_14 AC 149 ms
4,440 KB
testcase_15 AC 156 ms
4,348 KB
testcase_16 AC 73 ms
4,440 KB
testcase_17 AC 82 ms
4,968 KB
testcase_18 AC 88 ms
4,968 KB
testcase_19 AC 177 ms
4,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <cstring>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define rep(i,n) for (int i = 0; i < int(n);i++)
using mint = modint998244353;

int main(){
  int n,m;
  cin >> n >> m;
  vector<mint> a(n);
  dsu d(n);
  for (int i = 0;i < n;i++){
    ll x;
    cin >> x;
    a[i] = mint(x);
  }
  for (int i = 0; i < m;i++){
    int u,v;
    cin >> u >> v;
    u--;v--;
    int lu = d.leader(u);
    int lv = d.leader(v);
    if (!d.same(u,v)){
      d.merge(u,v);
      if (d.leader(u) == lv){
        a[lv] += a[lu];
      }
      else if (d.leader(u) == lu){
        a[lu] += a[lv];
      }
    }
  }
  mint ans = 1;
  for (int i = 0; i < n;i++){
    ans *= a[d.leader(i)];
  }
  cout << ans.val() << endl;

  return 0;
}
0