結果

問題 No.2236 Lights Out On Simple Graph
ユーザー 👑 chro_96chro_96
提出日時 2023-03-03 22:25:12
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 692 ms / 4,000 ms
コード長 1,835 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 31,704 KB
実行使用メモリ 34,404 KB
最終ジャッジ日時 2023-10-18 02:35:38
合計ジャッジ時間 20,113 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
18,352 KB
testcase_01 AC 6 ms
18,288 KB
testcase_02 AC 6 ms
18,352 KB
testcase_03 AC 470 ms
33,612 KB
testcase_04 AC 318 ms
22,152 KB
testcase_05 AC 6 ms
18,352 KB
testcase_06 AC 419 ms
26,248 KB
testcase_07 AC 474 ms
34,404 KB
testcase_08 AC 6 ms
18,288 KB
testcase_09 AC 6 ms
18,352 KB
testcase_10 AC 112 ms
22,140 KB
testcase_11 AC 72 ms
20,096 KB
testcase_12 AC 164 ms
22,140 KB
testcase_13 AC 17 ms
18,776 KB
testcase_14 AC 344 ms
26,228 KB
testcase_15 AC 13 ms
18,512 KB
testcase_16 AC 21 ms
18,776 KB
testcase_17 AC 57 ms
20,096 KB
testcase_18 AC 6 ms
18,380 KB
testcase_19 AC 38 ms
19,040 KB
testcase_20 AC 230 ms
26,228 KB
testcase_21 AC 37 ms
19,040 KB
testcase_22 AC 466 ms
34,404 KB
testcase_23 AC 481 ms
26,248 KB
testcase_24 AC 381 ms
24,200 KB
testcase_25 AC 80 ms
20,096 KB
testcase_26 AC 465 ms
34,404 KB
testcase_27 AC 230 ms
26,228 KB
testcase_28 AC 360 ms
26,228 KB
testcase_29 AC 37 ms
19,304 KB
testcase_30 AC 464 ms
34,404 KB
testcase_31 AC 463 ms
34,404 KB
testcase_32 AC 459 ms
34,404 KB
testcase_33 AC 470 ms
33,612 KB
testcase_34 AC 461 ms
34,404 KB
testcase_35 AC 511 ms
34,404 KB
testcase_36 AC 614 ms
32,392 KB
testcase_37 AC 475 ms
34,404 KB
testcase_38 AC 692 ms
34,404 KB
testcase_39 AC 630 ms
34,404 KB
testcase_40 AC 291 ms
25,700 KB
testcase_41 AC 38 ms
19,304 KB
testcase_42 AC 165 ms
22,140 KB
testcase_43 AC 16 ms
18,776 KB
testcase_44 AC 542 ms
34,404 KB
testcase_45 AC 473 ms
34,404 KB
testcase_46 AC 458 ms
34,404 KB
testcase_47 AC 503 ms
34,404 KB
testcase_48 AC 469 ms
34,404 KB
testcase_49 AC 466 ms
34,404 KB
testcase_50 AC 14 ms
18,512 KB
testcase_51 AC 110 ms
22,140 KB
testcase_52 AC 14 ms
18,512 KB
testcase_53 AC 6 ms
18,288 KB
testcase_54 AC 351 ms
26,228 KB
testcase_55 AC 519 ms
34,404 KB
testcase_56 AC 539 ms
34,404 KB
testcase_57 AC 681 ms
34,404 KB
testcase_58 AC 522 ms
34,404 KB
testcase_59 AC 546 ms
34,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

int cmp_ll_alt (const void *a, const void *b) {
  long long *a_ = (long long *)a;
  long long *b_ = (long long *)b;
  
  if (a_[0] < b_[0]) {
    return -1;
  }
  
  if (a_[0] > b_[0]) {
    return 1;
  }
  
  if (a_[1] < b_[1]) {
    return 1;
  }
  
  if (a_[1] > b_[1]) {
    return -1;
  }
  
  return 0;
}

int main () {
  int n = 0;
  int m = 0;
  long long a[40] = {};
  long long b[40] = {};
  long long c = 0LL;
  
  int res = 0;
  
  int ans = 0;
  
  long long cbit = 0LL;
  long long half[1048576][2] = {};
  
  res = scanf("%d", &n);
  res = scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    res = scanf("%lld", a+i);
    res = scanf("%lld", b+i);
    a[i] -= 1LL;
    b[i] -= 1LL;
  }
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", &c);
    cbit |= (c<<((long long)i));
  }
  
  ans = m+1;
  
  for (int i = 0; i < (1<<(m/2)); i++) {
    for (int j = 0; j < m/2; j++) {
      if ((i&(1<<j)) > 0) {
        half[i][1] += 1LL;
        half[i][0] ^= (1LL<<a[j]);
        half[i][0] ^= (1LL<<b[j]);
      }
    }
  }
  
  qsort(half, (1<<(m/2)), sizeof(long long)*2, cmp_ll_alt);
  
  for (int i = 0; i < (1<<(m-m/2)); i++) {
    long long hbit = 0LL;
    int cnt = 0;
    int idx[2] = { 0, (1<<m/2) };
    for (int j = 0; j < m-m/2; j++) {
      if ((i&(1<<j)) > 0) {
        cnt++;
        hbit ^= (1LL<<a[j+m/2]);
        hbit ^= (1LL<<b[j+m/2]);
      }
    }
    while (idx[1] - idx[0] > 1) {
      int nxt = (idx[0]+idx[1])/2;
      if (half[nxt][0] <= (cbit^hbit)) {
        idx[0] = nxt;
      } else {
        idx[1] = nxt;
      }
    }
    if (half[idx[0]][0] == (cbit^hbit) && ans > cnt+((int)half[idx[0]][1])) {
      ans = cnt+((int)half[idx[0]][1]);
    }
  }
  
  if (ans > m) {
    printf("-1\n");
    return 0;
  }
  
  printf("%d\n", ans);
  return 0;
}
0