結果

問題 No.2236 Lights Out On Simple Graph
ユーザー prism17
提出日時 2023-03-24 13:30:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 991 ms / 4,000 ms
コード長 1,798 bytes
コンパイル時間 2,039 ms
コンパイル使用メモリ 179,992 KB
実行使用メモリ 68,992 KB
最終ジャッジ日時 2024-09-18 16:23:06
合計ジャッジ時間 23,805 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

// Problem: No.2236 Lights Out On Simple GraphNo.2236 熄灯简图
// Contest: yukicoder
// URL: https://yukicoder.me/problems/no/2236
// Memory Limit: 512 MB
// Time Limit: 4000 ms

#include <bits/stdc++.h>

#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define dbg(x) cout << #x << " = " << (x) << "\n";
#define popcount(x) __builtin_popcountll((x))
#define all(v) (v).begin(), (v).end()
#define pb emplace_back
#define x first
#define y second

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m;
ll st;

void solve() {
  cin >> n >> m;
  vector<pll> edges(m);
  for (auto &e : edges) {
    cin >> e.x >> e.y;
    e.x--, e.y--;
  }
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    st |= x << i;
  }
  int nn = m / 2;
  map<ll, int> vis;
  for (ll j = 0; j < (1ll << nn); j++) {
    ll state = 0;
    for (int k = 0; k < nn; k++) {
      if (j & (1ll << k)) {
        int x = edges[k].x, y = edges[k].y;
        state ^= (1ll << x);
        state ^= (1ll << y);
      }
    }
    if (vis.count(state))
      vis[state] = min(vis[state], popcount(j));
    else
      vis[state] = popcount(j);
  }
  int rest = m - nn;
  int ans = -1;
  for (ll j = 0; j < (1ll << rest); j++) {
    ll state = 0;
    for (int k = 0; k < rest; k++) {
      if (j & (1ll << k)) {
        int x = edges[nn + k].x, y = edges[nn + k].y;
        state ^= (1ll << x);
        state ^= (1ll << y);
      }
    }
    if (vis.count(state ^ st)) {
      if (ans == -1)
        ans = vis[state ^ st] + popcount(j);
      else
        ans = min(vis[state ^ st] + popcount(j), ans);
    }
  }
  cout << ans << "\n";
}

int main() {
  fastio;
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
0