結果

問題 No.2277 Honest or Dishonest ?
ユーザー 👑 chro_96chro_96
提出日時 2023-04-21 21:55:17
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 2,105 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 32,360 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2024-04-25 18:56:29
合計ジャッジ時間 3,302 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,252 KB
testcase_01 AC 4 ms
9,212 KB
testcase_02 AC 4 ms
9,216 KB
testcase_03 AC 34 ms
9,212 KB
testcase_04 AC 35 ms
9,464 KB
testcase_05 AC 12 ms
9,168 KB
testcase_06 AC 30 ms
9,284 KB
testcase_07 AC 25 ms
9,236 KB
testcase_08 AC 11 ms
9,216 KB
testcase_09 AC 23 ms
9,360 KB
testcase_10 AC 20 ms
9,212 KB
testcase_11 AC 11 ms
9,100 KB
testcase_12 AC 19 ms
9,128 KB
testcase_13 AC 37 ms
9,212 KB
testcase_14 AC 23 ms
9,084 KB
testcase_15 AC 10 ms
9,216 KB
testcase_16 AC 23 ms
9,216 KB
testcase_17 AC 13 ms
9,292 KB
testcase_18 AC 31 ms
9,344 KB
testcase_19 AC 32 ms
9,452 KB
testcase_20 AC 27 ms
9,344 KB
testcase_21 AC 25 ms
9,216 KB
testcase_22 AC 22 ms
9,344 KB
testcase_23 AC 30 ms
9,160 KB
testcase_24 AC 21 ms
9,340 KB
testcase_25 AC 23 ms
9,344 KB
testcase_26 AC 7 ms
9,212 KB
testcase_27 AC 17 ms
9,160 KB
testcase_28 AC 11 ms
9,276 KB
testcase_29 AC 18 ms
9,116 KB
testcase_30 AC 17 ms
9,212 KB
testcase_31 AC 26 ms
9,316 KB
testcase_32 AC 15 ms
9,088 KB
testcase_33 AC 32 ms
9,404 KB
testcase_34 AC 36 ms
9,216 KB
testcase_35 AC 5 ms
9,108 KB
testcase_36 AC 24 ms
9,316 KB
testcase_37 AC 35 ms
9,228 KB
testcase_38 AC 11 ms
9,212 KB
testcase_39 AC 13 ms
9,216 KB
testcase_40 AC 6 ms
9,208 KB
testcase_41 AC 39 ms
9,340 KB
testcase_42 AC 28 ms
9,216 KB
testcase_43 AC 39 ms
9,336 KB
testcase_44 AC 39 ms
9,452 KB
testcase_45 AC 39 ms
9,468 KB
testcase_46 AC 37 ms
9,444 KB
testcase_47 AC 38 ms
9,400 KB
testcase_48 AC 37 ms
9,600 KB
testcase_49 AC 38 ms
9,472 KB
testcase_50 AC 38 ms
9,468 KB
testcase_51 AC 39 ms
9,460 KB
testcase_52 AC 37 ms
9,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct list {
  int v;
  struct list *n;
} list;

typedef struct tree {
  int 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 func (int v, int b, list **e, int *flag, int *visited) {
  list *l = e[v];
  
  if (visited[v] > 0 && flag[v] != b) {
    return -1;
  } else if (visited[v] > 0) {
    return 1;
  }
  
  visited[v] = 1;
  flag[v] = b;
  while (l != NULL) {
    if (func(l->v, 1-b, e, flag, visited) < 0) {
      return -1;
    }
    l = l->n;
  }
  
  return 1;
}

int main () {
  int n = 0;
  int q = 0;
  int a[100000] = {};
  int b[100000] = {};
  int c[100000] = {};
  
  int res = 0;
  
  long long ans = 1LL;
  long long mod_num = 998244353LL;
  
  tree t[100000] = {};
  
  list pool[200000] = {};
  int used = 0;
  
  list *e[100000] = {};
  int flag[100000] = {};
  int visited[100000] = {};
  
  res = scanf("%d", &n);
  res = scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    res = scanf("%d", a+i);
    res = scanf("%d", b+i);
    res = scanf("%d", c+i);
    a[i]--;
    b[i]--;
  }
  
  for (int i = 0; i < n; i++) {
    t[i].v = i;
    t[i].p = NULL;
  }
  
  for (int i = 0; i < q; i++) {
    if (c[i] == 0) {
      tree *rt1 = get_root(t+a[i]);
      tree *rt2 = get_root(t+b[i]);
      if (rt1 != rt2) {
        rt2->p = rt1;
      }
    }
  }
  
  for (int i = 0; i < q; i++) {
    if (c[i] == 1) {
      tree *rt1 = get_root(t+a[i]);
      tree *rt2 = get_root(t+b[i]);
      if (rt1 == rt2) {
        ans = 0LL;
      } else {
        pool[used].v = rt2->v;
        pool[used].n = e[rt1->v];
        e[rt1->v] = pool+used;
        used++;
        pool[used].v = rt1->v;
        pool[used].n = e[rt2->v];
        e[rt2->v] = pool+used;
        used++;
      }
    }
  }
  
  for (int i = 0; i < n; i++) {
    if (t[i].p == NULL && visited[i] <= 0) {
      if (func(i, 0, e, flag, visited) < 0) {
        ans = 0LL;
      } else {
        ans *= 2LL;
        ans %= mod_num;
      }
    }
  }
  
  printf("%lld\n", ans);
  return 0;
}
0