結果

問題 No.2236 Lights Out On Simple Graph
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-02-27 00:55:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 837 ms / 4,000 ms
コード長 1,563 bytes
コンパイル時間 1,329 ms
コンパイル使用メモリ 107,716 KB
実行使用メモリ 85,232 KB
最終ジャッジ日時 2023-10-15 02:38:20
合計ジャッジ時間 28,053 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 725 ms
85,080 KB
testcase_04 AC 484 ms
76,888 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 507 ms
85,232 KB
testcase_07 AC 768 ms
84,832 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 1 ms
4,372 KB
testcase_10 AC 155 ms
15,320 KB
testcase_11 AC 71 ms
5,132 KB
testcase_12 AC 277 ms
23,564 KB
testcase_13 AC 20 ms
5,956 KB
testcase_14 AC 598 ms
44,084 KB
testcase_15 AC 17 ms
5,484 KB
testcase_16 AC 31 ms
7,792 KB
testcase_17 AC 88 ms
9,080 KB
testcase_18 AC 3 ms
4,372 KB
testcase_19 AC 73 ms
12,260 KB
testcase_20 AC 338 ms
44,048 KB
testcase_21 AC 67 ms
12,260 KB
testcase_22 AC 666 ms
84,960 KB
testcase_23 AC 837 ms
85,020 KB
testcase_24 AC 569 ms
43,932 KB
testcase_25 AC 136 ms
21,608 KB
testcase_26 AC 729 ms
84,980 KB
testcase_27 AC 372 ms
44,016 KB
testcase_28 AC 704 ms
76,988 KB
testcase_29 AC 64 ms
12,408 KB
testcase_30 AC 676 ms
85,088 KB
testcase_31 AC 668 ms
85,016 KB
testcase_32 AC 707 ms
85,216 KB
testcase_33 AC 714 ms
84,972 KB
testcase_34 AC 597 ms
84,964 KB
testcase_35 AC 829 ms
52,524 KB
testcase_36 AC 563 ms
23,564 KB
testcase_37 AC 779 ms
85,124 KB
testcase_38 AC 678 ms
23,884 KB
testcase_39 AC 513 ms
20,128 KB
testcase_40 AC 304 ms
19,432 KB
testcase_41 AC 70 ms
8,308 KB
testcase_42 AC 320 ms
39,884 KB
testcase_43 AC 16 ms
4,744 KB
testcase_44 AC 656 ms
36,036 KB
testcase_45 AC 738 ms
84,844 KB
testcase_46 AC 620 ms
52,164 KB
testcase_47 AC 780 ms
85,048 KB
testcase_48 AC 584 ms
35,732 KB
testcase_49 AC 811 ms
84,960 KB
testcase_50 AC 13 ms
4,372 KB
testcase_51 AC 177 ms
23,544 KB
testcase_52 AC 13 ms
4,368 KB
testcase_53 AC 1 ms
4,368 KB
testcase_54 AC 539 ms
27,504 KB
testcase_55 AC 592 ms
23,508 KB
testcase_56 AC 590 ms
23,364 KB
testcase_57 AC 826 ms
27,632 KB
testcase_58 AC 464 ms
20,776 KB
testcase_59 AC 492 ms
20,836 KB
権限があれば一括ダウンロードができます

ソースコード

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