結果

問題 No.2236 Lights Out On Simple Graph
ユーザー srjywrdnprkt
提出日時 2023-02-27 00:55:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 899 ms / 4,000 ms
コード長 1,563 bytes
コンパイル時間 1,293 ms
コンパイル使用メモリ 108,976 KB
最終ジャッジ日時 2025-02-10 23:46:44
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <cassert>

using namespace std;
using ll = long long;

int main(){

    ll N, M, m, a, b, vtx, ans=1e9, c=0, mx;
    cin >> N >> M;

    assert(2 <= N && N <= 40);
    assert(1 <= M && M <= min(N*(N-1)/2, 40LL));

    m = M/2;
    set<pair<ll, ll>> st;
    vector<pair<ll, ll>> e(M);
    for (int i=0; i<M; i++){
        cin >> a >> b; a--; b--;
        e[i] = {a, b};
    
        assert(!st.count({a, b}));
        assert(a != b);
        st.insert({a, b});
    }

    for (int i=0; i<N; i++){
        cin >> a;
        assert(a == 0 || a == 1);
        c |= a*(1LL<<i);
    }

    vector<pair<ll, ll>> v;
    map<ll, ll> mp;

    mx = 1<<m;
    for (int i=0; i<mx; i++){
        vtx = 0;
        for (int j=0; j<m; j++){
            if (i & (1<<j)){
                tie(a, b) = e[j];
                vtx ^= (1LL<<a); vtx ^= (1LL<<b);
            }
        }
        v.push_back({vtx, __builtin_popcount(i)});
    }

    mx = 1<<(M-m);
    for (int i=0; i<mx; i++){
        vtx = 0;
        for (int j=0; j<(M-m); j++){
            if (i & (1<<j)){
                tie(a, b) = e[m+j];
                vtx ^= (1LL<<a); vtx ^= (1LL<<b);
            }   
        }
        if (mp.count(vtx)) mp[vtx] = min(mp[vtx], (ll)__builtin_popcount(i));
        else mp[vtx] = __builtin_popcount(i);
    }

    for (auto [vtx, cnt] : v){
        if (mp.count(vtx ^ c)) ans = min(ans, cnt+mp[vtx ^ c]);
    }

    if (ans == 1e9) ans = -1;
    cout << ans << endl;

    return 0;
}
0