結果

問題 No.2236 Lights Out On Simple Graph
ユーザー milanis48663220
提出日時 2023-03-03 22:35:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,281 ms / 4,000 ms
コード長 2,450 bytes
コンパイル時間 1,529 ms
コンパイル使用メモリ 122,332 KB
最終ジャッジ日時 2025-02-11 03:17:51
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    vector<int> a(m), b(m);
    for(int i = 0; i < m; i++){
        cin >> a[i] >> b[i]; a[i]--; b[i]--;
    }
    ll s = 0;
    for(int i = 0; i < n; i++){
        int c; cin >> c;
        if(c == 1) s += 1ll<<i;
    }
    int k = m/2;
    unordered_map<ll, int> mp;
    for(int s = 0; s < (1<<k); s++){
        ll x = 0;
        int cnt = 0;
        for(int i = 0; i < k; i++){
            if(s&(1<<i)){
                x ^= 1ll<<a[i];
                x ^= 1ll<<b[i];
                cnt++;
            }
        }
        if(mp.count(x)) chmin(mp[x], cnt);
        else mp[x] = cnt;
    }
    // for(auto [s, cnt]: mp) cout << s << ':' << cnt << endl;
    int kk = m-k;
    ll target = s;
    int ans = 100000;
    for(int s = 0; s < (1<<kk); s++){
        ll x = 0;
        int cnt = 0;
        for(int i = 0; i < kk; i++){
            if(s&(1<<i)){
                x ^= 1ll<<a[i+k];
                x ^= 1ll<<b[i+k];
                cnt++;
            }
        }
        ll y = x^target;
        if(mp.count(y)){
            chmin(ans, cnt+mp[y]);
        }
    }
    if(ans == 100000) cout << -1 << endl;
    else cout << ans << endl;
}
0