結果

問題 No.2536 同値性と充足可能性
ユーザー 👑 chro_96chro_96
提出日時 2023-11-10 22:51:11
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 2,582 bytes
コンパイル時間 1,227 ms
コンパイル使用メモリ 32,256 KB
実行使用メモリ 10,296 KB
最終ジャッジ日時 2023-11-10 22:51:14
合計ジャッジ時間 2,621 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,296 KB
testcase_01 AC 4 ms
10,168 KB
testcase_02 AC 4 ms
10,296 KB
testcase_03 AC 4 ms
10,168 KB
testcase_04 AC 4 ms
10,296 KB
testcase_05 AC 4 ms
10,296 KB
testcase_06 AC 4 ms
10,296 KB
testcase_07 AC 4 ms
10,296 KB
testcase_08 AC 4 ms
10,296 KB
testcase_09 AC 4 ms
10,296 KB
testcase_10 AC 3 ms
10,296 KB
testcase_11 AC 4 ms
10,296 KB
testcase_12 AC 4 ms
10,296 KB
testcase_13 AC 4 ms
10,296 KB
testcase_14 AC 4 ms
10,296 KB
testcase_15 AC 4 ms
10,296 KB
testcase_16 AC 5 ms
10,296 KB
testcase_17 AC 4 ms
10,296 KB
testcase_18 AC 4 ms
10,296 KB
testcase_19 AC 4 ms
10,168 KB
testcase_20 AC 6 ms
10,168 KB
testcase_21 AC 7 ms
10,168 KB
testcase_22 AC 7 ms
10,168 KB
testcase_23 AC 33 ms
10,168 KB
testcase_24 AC 33 ms
10,168 KB
testcase_25 AC 47 ms
10,296 KB
testcase_26 AC 49 ms
10,296 KB
testcase_27 AC 45 ms
10,296 KB
testcase_28 AC 47 ms
10,296 KB
testcase_29 AC 46 ms
10,296 KB
testcase_30 AC 44 ms
10,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct tree {
  int v;
  int n;
  struct tree *p;
} tree;

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

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, list **e, int tmp_p, int *p, int *cnt, tree *t, int hd) {
  list *l = e[v];
  
  if (p[v] > 0 && tmp_p != (p[v]-1)%2+1) {
    return 0;
  }
  
  if (p[v] > 0) {
    return 1;
  }
  
  p[v] = hd*2+tmp_p;
  cnt[tmp_p-1] += t[v].n;
  while (l != NULL) {
    if (func(l->v, e, 3-tmp_p, p, cnt, t, hd) <= 0) {
      return 0;
    }
    l = l->n;
  }
  
  return 1;
}

int main () {
  int n = 0;
  int m = 0;
  int i[100000] = {};
  char e[100000][6] = {};
  int j[100000] = {};
  
  int res = 0;
  
  int is_ok = 1;
  int ans[100000] = {};
  int anscnt = 0;
  
  tree t[100000] = {};
  
  list *el[100000] = {};
  list pool[200000] = {};
  int used = 0;
  
  int p[100000] = {};
  int flag[200000] = {};
  
  res = scanf("%d", &n);
  res = scanf("%d", &m);
  for (int k = 0; k < m; k++) {
    res = scanf("%d", i+k);
    res = scanf("%s", e[k]);
    res = scanf("%d", j+k);
    i[k]--;
    j[k]--;
  }
  
  for (int k = 0; k < n; k++) {
    t[k].v = k;
    t[k].n = 1;
    t[k].p = NULL;
  }
  
  for (int k = 0; k < m; k++) {
    if (e[k][2] != '/') {
      tree *rt1 = get_root(t+i[k]);
      tree *rt2 = get_root(t+j[k]);
      if (rt1 != rt2) {
        rt1->n += rt2->n;
        rt2->p = rt1;
      }
    }
  }
  
  for (int k = 0; k < m; k++) {
    if (e[k][2] == '/') {
      tree *rt1 = get_root(t+i[k]);
      tree *rt2 = get_root(t+j[k]);
      if (rt1 == rt2) {
        is_ok = 0;
      } else {
        pool[used].v = rt2->v;
        pool[used].n = el[rt1->v];
        el[rt1->v] = pool+used;
        used++;
        pool[used].v = rt1->v;
        pool[used].n = el[rt2->v];
        el[rt2->v] = pool+used;
        used++;
      }
    }
  }
  
  for (int k = 0; k < n; k++) {
    if (is_ok > 0 && t[k].p == NULL && p[k] <= 0) {
      int cnt[2] = {};
      is_ok &= func(k, el, 1, p, cnt, t, k);
      if (cnt[0] < cnt[1]) {
        flag[2*k+1] = 1;
      } else {
        flag[2*k] = 1;
      }
    }
  }
  
  if (is_ok <= 0) {
    printf("No\n");
    return 0;
  }
  
  for (int k = 0; k < n; k++) {
    tree *rt = get_root(t+k);
    if (flag[p[rt->v]-1] > 0) {
      ans[anscnt] = k+1;
      anscnt++;
    }
  }
  
  printf("Yes\n");
  printf("%d\n", anscnt);
  printf("%d", ans[0]);
  for (int k = 1; k < anscnt; k++) {
    printf(" %d", ans[k]);
  }
  printf("\n");
  
  return 0;
}
0