結果

問題 No.2494 Sum within Components
ユーザー KumaTachiRenKumaTachiRen
提出日時 2023-09-01 10:36:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 4,358 ms
コンパイル使用メモリ 265,556 KB
実行使用メモリ 16,788 KB
最終ジャッジ日時 2023-09-01 10:37:27
合計ジャッジ時間 5,967 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 8 ms
4,388 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 11 ms
4,476 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 83 ms
12,892 KB
testcase_15 AC 84 ms
11,020 KB
testcase_16 AC 35 ms
10,284 KB
testcase_17 AC 32 ms
11,744 KB
testcase_18 AC 38 ms
12,556 KB
testcase_19 AC 107 ms
16,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

struct Fast {
  Fast() {
    std::cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(10);
  }
} fast;

#define rep(i, a, b) for (int(i) = (a); (i) < (int)(b); (i)++)

using mint = modint998244353;

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> a(n);
  rep(i, 0, n) cin >> a[i];

  vector<vector<int>> g(n, vector<int>());
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  vector<bool> seen(n, false);
  vector<int> root(n, -1), size(n, 0);
  vector<mint> sum(n, 0);
  stack<int> st;
  rep(i, 0, n) st.push(i);
  while (!st.empty()) {
    int x = st.top();
    st.pop();
    if (seen[x]) continue;
    seen[x] = true;
    if (root[x] == -1) root[x] = x;
    sum[root[x]] += a[x];
    size[root[x]]++;
    for (auto y : g[x]) {
      if (root[y] != -1) continue;
      root[y] = root[x];
      st.push(y);
    }
  }

  mint ans = 1;
  rep(i, 0, n) {
    if (root[i] != i) continue;
    rep(j, 0, size[i]) ans *= sum[i];
  }
  cout << ans.val() << endl;
}
0