結果

問題 No.2494 Sum within Components
ユーザー 👑 chro_96chro_96
提出日時 2023-10-06 22:11:49
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,003 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 29,312 KB
実行使用メモリ 5,052 KB
最終ジャッジ日時 2023-10-06 22:11:51
合計ジャッジ時間 1,665 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,956 KB
testcase_01 AC 2 ms
4,848 KB
testcase_02 AC 2 ms
4,976 KB
testcase_03 AC 3 ms
4,968 KB
testcase_04 AC 2 ms
4,996 KB
testcase_05 AC 2 ms
4,912 KB
testcase_06 AC 2 ms
4,912 KB
testcase_07 AC 2 ms
4,908 KB
testcase_08 AC 3 ms
4,844 KB
testcase_09 AC 7 ms
5,032 KB
testcase_10 AC 7 ms
5,044 KB
testcase_11 AC 3 ms
4,912 KB
testcase_12 AC 10 ms
4,960 KB
testcase_13 AC 6 ms
5,000 KB
testcase_14 AC 63 ms
5,052 KB
testcase_15 AC 75 ms
4,952 KB
testcase_16 AC 28 ms
4,964 KB
testcase_17 AC 29 ms
4,956 KB
testcase_18 AC 34 ms
4,844 KB
testcase_19 AC 73 ms
5,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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