結果

問題 No.2236 Lights Out On Simple Graph
ユーザー hiro71687k
提出日時 2023-03-06 00:41:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,551 bytes
コンパイル時間 3,818 ms
コンパイル使用メモリ 257,096 KB
最終ジャッジ日時 2025-02-11 05:41:12
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 55 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=10010010010010010;
ll mod=998244353;
int main(){
    ll n,m;
    cin >> n >> m;
    vector<ll>a(m),b(m);
    for (ll i = 0; i < m; i++)
    {
        cin >> a[i] >> b[i];
        a[i]--,b[i]--;
    }
    vector<ll>c(n);
    vector<ll>two(50,1);
    for (ll i = 1; i < 50; i++)
    {
        two[i]=two[i-1]*2;
    }
    ll now=0;
    for (ll i = 0; i < n; i++)
    {
        cin >> c[i];
        now+=two[i]*c[i];
    }
    if (m==1)
    {
        bool ok=true;
        ll ans=0;
        for (ll i = 0; i < n; i++)
        {
            if (c[i]==1)
            {
                if ((a[0]==c[i]||b[0]==c[i])&&c[a[0]]==1&&c[b[0]]==1)
                {
                    c[a[0]]=0;
                    c[b[0]]=0;
                    ans+=1;
                }else{
                    ok=false;
                }
            }
        }
        if (ok)
        {
            cout << ans << endl;
        }else{
            cout << -1 << endl;
        }
        return 0;
    }
    
    map<ll,ll>dp1,dp2;
    ll x=m/2;
    for (ll i = 0; i < two[x]; i++)
    {
        ll y=now;
        ll kai=0;
        for (ll j = 0; j < x; j++)
        {
            if (i&two[j])
            {
                y^=two[a[j]];
                y^=two[b[j]];
                kai+=1;
            }
        }
        if (y==now)
        {
            dp1[y]=0;
        }else{
            if (dp1[y]==0)
            {
                dp1[y]=kai;
            }else{
                dp1[y]=min(dp1[y],kai);
            }
        }
    }
    x=m-x;
    for (ll i = 0; i < two[x]; i++)
    {
        ll y=0;
        ll kai=0;
        for (ll j = m/2; j < m; j++)
        {
            if (i&two[j-m/2])
            {
                y^=two[a[j]];
                y^=two[b[j]];
                kai+=1;
            }
        }
        if (y==now)
        {
            dp2[y]=0;
        }else{
            if (dp2[y]==0)
            {
                dp2[y]=kai;
            }else{
                dp2[y]=min(dp2[y],kai);
            }
        }
    }
    ll ans=inf;
    for(auto v:dp1){
        if (v.first==0)
        {
            ans=min(ans,v.second);
        }else{
            if (dp2[v.first]!=0)
            {
                ans=min(ans,v.second+dp2[v.first]);
            }
        }
    }
    if (ans==inf)
    {
        cout << -1 << endl;
        return 0;
    }
    
    cout << ans <<endl;
}
0