結果

問題 No.2494 Sum within Components
コンテスト
ユーザー chro_96
提出日時 2023-10-06 22:11:49
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,003 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 189 ms
コンパイル使用メモリ 39,524 KB
最終ジャッジ日時 2026-02-22 11:17:08
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <stdio.h>

typedef struct tree {
  long long v;
  struct tree *p;
} tree;

tree *get_root (tree *t) {
  if (t == NULL || t->p == NULL) {
    return t;
  }
  
  t->p = get_root(t->p);
  return t->p;
}

int main () {
  int n = 0;
  int m = 0;
  long long a = 0LL;
  int u = 0;
  int v = 0;
  
  int res = 0;
  
  long long ans = 1LL;
  long long mod_num = 998244353LL;
  
  tree t[200000] = {};
  
  res = scanf("%d", &n);
  res = scanf("%d", &m);
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", &a);
    t[i].v = a;
    t[i].p = NULL;
  }
  for (int i = 0; i < m; i++) {
    tree *rt1 = NULL;
    tree *rt2 = NULL;
    res = scanf("%d", &u);
    res = scanf("%d", &v);
    u--;
    v--;
    rt1 = get_root(t+u);
    rt2 = get_root(t+v);
    if (rt1 != rt2) {
      rt1->v += rt2->v;
      rt1->v %= mod_num;
      rt2->p = rt1;
    }
  }
  
  for (int i = 0; i < n; i++) {
    tree *rt = get_root(t+i);
    ans *= rt->v;
    ans %= mod_num;
  }
  
  printf("%lld\n", ans);
  return 0;
}
0