Description###

Bessie and the cows were playing games in the barn, but the power was reset and the lights were all turned off. Help the cows get all the lights back on so they can resume their games.

The N (1 <= N <= 35) lights conveniently numbered 1..N and their switches are arranged in a complex network with M (1 <= M <= 595) clever connection between pairs of lights (see below).

Each light has a switch that, when toggled, causes that light -- and all of the lights that are connected to it -- to change their states (from on to off, or off to on).

Find the minimum number of switches that need to be toggled in order to turn all the lights back on.

It's guaranteed that there is at least one way to toggle the switches so all lights are back on.

贝希和她的闺密们在她们的牛棚中玩游戏。但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了。贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望。她希望您能够帮帮她,把所有的灯都给重新开起来!她才能继续快乐地跟她的闺密们继续玩游戏! 牛棚中一共有N(1 <= N <= 35)盏灯,编号为1到N。这些灯被置于一个非常複杂的网络之中。有M(1 <= M <= 595)条很神奇的无向边,每条边连接两盏灯。 每盏灯上面都带有一个开关。当按下某一盏灯的开关的时候,这盏灯本身,还有所有有边连向这盏灯的灯的状态都会被改变。状态改变指的是:当一盏灯是开著的时候,这盏灯被关掉;当一盏灯是关著的时候,这盏灯被打开。 问最少要按下多少个开关,才能把所有的灯都给重新打开。 数据保证至少有一种按开关的方案,使得所有的灯都被重新打开。

Input###

Line 1: Two space-separated integers: N and M.

Lines 2..M+1: Each line contains two space-separated integers representing two lights that are connected. No pair will be repeated.

Output###

Line 1: A single integer representing the minimum number of switches that need to be flipped in order to turn on all the lights.

Sample Input###

5 6

1 2

1 3

4 2

3 4

2 5

5 3

Sample Output###

3

HINT###

There are 5 lights. Lights 1, 4, and 5 are each connected to both lights 2 and 3.

Toggle the switches on lights 1, 4, and 5.


想法##

异或方程组

首先,显然每个灯最多只会被开一次。那么设x[i]表示第i盏灯是否被开

那可以列n个形如 (map[1][i] * x[1])^(map[2][i] * x[2])(map[n][i] * x[n])=1 的异或方程,表示这盏灯最终的状态为开

然后先用高斯消元解一下,但可能有些x[]前面的系数为0

在遇到这种情况时需要对于这个x是0还是1进行dfs


代码##

#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; const int N = 40;
typedef int Mat[N][N]; int n,m; void gauss(Mat A){
for(int i=1;i<=n;i++){
int r=i;
for(int j=i+1;j<=n;j++)
if(A[j][i]>A[r][i]) r=j;
for(int j=i;j<=n+1;j++) swap(A[i][j],A[r][j]);
if(!A[i][i]) continue;
for(int j=i+1;j<=n;j++)
if(A[j][i])
for(int k=i;k<=n+1;k++) A[j][k]^=A[i][k];
}
} Mat a;
int ans,val[N];
void dfs(int u,int c){
if(c>ans) return;
if(u==0) {
ans=c;
return;
}
if(a[u][u]){
val[u]=a[u][n+1];
for(int j=u+1;j<=n;j++)
if(a[u][j]) val[u]^=val[j];
dfs(u-1,c+val[u]);
}
else{
val[u]=1;
dfs(u-1,c+1);
val[u]=0;
dfs(u-1,c);
}
} int main()
{
int x,y;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
a[x][y]=a[y][x]=1;
}
for(int i=1;i<=n;i++) a[i][n+1]=a[i][i]=1; gauss(a);
ans=n+1;
dfs(n,0); printf("%d",ans); return 0;
}

最新文章

  1. Android studio安装
  2. Android中处理崩溃异常和记录日志
  3. json pickle time
  4. &lt;&lt;软技能,代码之外的生存技能&gt;&gt;读书笔记
  5. PhoneGap--001 入门 安装
  6. 查看linux中某个端口(port)是否被占用(netstat,lsof)
  7. React属性的3种设置方式
  8. javaweb学习总结(三十三)——使用JDBC对数据库进行CRUD
  9. JavaScript-每隔5分钟执行一次ajax请求的实现方法
  10. ubuntu下集群设置静态ip
  11. SSH反向连接让外网也可远程访问内网机器
  12. ios新开发语言swift 新手教程
  13. C#中静态和非静态的区别
  14. 乘积最大洛谷p1018
  15. HttpClient4.2 Fluent API学习
  16. python之路--day6--字符编码
  17. headfirst设计模式(8)—适配器模式与外观模式
  18. gitlab升级和迁移
  19. 在JSP中获取oracle中的时间戳类型的字段并显示
  20. hdu1159 dp(最长公共子序列)

热门文章

  1. C# 已知点和向量,求距离的点
  2. The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)
  3. F4与F1对比
  4. net core WebApi——依赖注入Autofac
  5. 22.XML
  6. 聚类分析 一、k-means
  7. Pycharm学生版安装教程(2019-12月更新)
  8. 1.1 Lack of free swap space on zabbix_server (zabbix监控报错)
  9. 基础之Lamada和Stream的邂逅
  10. 1031 查验身份证 (15 分)C语言