首先我们发现每个开关要不嗯1次,要不摁0次,没必要摁多次。于是对于每个点i,我们就得到了这样一个方程组:
a[i][1]*x[1]^a[i][2]*x[2]^…^a[i][n]*x[n]=1.
其中a[i][j]表示i,j是否有边,如果有就是1,否则是0.x[i]表示是否嗯开关i。然后我们用高斯消元解这个方程组,可以用bitset优化。然后爆搜枚举自由元,取最优解。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <bitset>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define N 40
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m,X[N],ans=inf;
bitset<N>a[N];
inline void Gauss(){
for(int i=1;i<=n;++i){
int r=i;
while(r<=n&&a[r][i]==0) ++r;
if(r>n) continue;
if(r!=i) swap(a[r],a[i]);
for(int j=i+1;j<=n;++j)
if(a[j][i]) a[j]^=a[i];
}
}
void dfs(int now,int tot){
if(tot>=ans) return;
if(!now){ans=tot;return;}
if(a[now][now]){
int x=a[now][n+1];
for(int i=now+1;i<=n;++i)
if(a[now][i]) x^=X[i];
X[now]=x;
dfs(now-1,tot+x);
}else{//是自由元
X[now]=0;dfs(now-1,tot);
X[now]=1;dfs(now-1,tot+1);
}
}
int main(){
// freopen("a.in","r",stdin);
n=read();m=read();
for(int i=1;i<=n;++i) a[i][i]=a[i][n+1]=1;
while(m--){
int x=read(),y=read();a[x][y]=1;a[y][x]=1;
}Gauss();dfs(n,0);
printf("%d\n",ans);
return 0;
}