题目描述

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)条很神奇的无向边,每条边连接两盏灯。 每盏灯上面都带有一个开关。当按下某一盏灯的开关的时候,这盏灯本身,还有所有有边连向这盏灯的灯的状态都会被改变。状态改变指的是:当一盏灯是开著的时候,这盏灯被关掉;当一盏灯是关著的时候,这盏灯被打开。 问最少要按下多少个开关,才能把所有的灯都给重新打开。 数据保证至少有一种按开关的方案,使得所有的灯都被重新打开。

输入输出格式

输入格式:

  • 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.

输出格式:

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

输入输出样例

输入样例#1:

5 6

1 2

1 3

4 2

3 4

2 5

5 3

输出样例#1:

3

说明

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.


高斯消元解异或方程组

矩阵的第i行表示的是按下第i个灯的开关后对其他灯的影响情况

C[i]表示这个灯选没选

样例的矩阵的第一行大概就长这样 :

然后高斯消元

这时的矩阵如果第i行的第i列是1就说明这是一个固定的选择

然后解一下这行的这个方程,解出是否选择这个灯

但是如果第i行的第i列是0 , 说明这一列是自由元

我们可以暴力枚举这些自由元是选还是不选

最后取一个代价最小的方案

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int M = 40 ;
const int INF = 2147483647 ;
using namespace std ; int n , m ;
int B[M][M] , Ans = INF , Chose[M] ;
void Gauss() {
for(int i = 1 ; i <= n ; i ++) {
int Tab = i ;
for(int j = i + 1 ; j <= n ; j ++)
if(B[j][i] > B[i][i])
Tab = j ;
for(int j = 1 ; j <= n + 1 ; j ++) swap(B[i][j] , B[Tab][j]) ;
for(int j = 1 ; j <= n ; j ++)
if(B[j][i] && j != i)
for(int k = i ; k <= n + 1 ; k ++)
B[j][k] ^= B[i][k] ;
}
}
void Dfs(int x , int Step) {
if(Step >= Ans) return ;
if(x == 0) {
Ans = Step ;
return ;
}
if(B[x][x]) {
int temp = B[x][n + 1] ;
for(int i = x + 1 ; i <= n ; i ++) temp ^= B[x][i] * Chose[i] ;
Chose[x] = temp ;
Dfs(x - 1 , (Step) + temp) ;
}
else {
Chose[x] = 0 ; Dfs(x - 1 , Step) ;
Chose[x] = 1 ; Dfs(x - 1 , Step + 1) ;
}
}
int main() {
scanf("%d%d",&n,&m) ;
for(int i = 1 , u , v ; i <= m ; i ++) {
scanf("%d%d",&u,&v) ;
B[u][v] = B[v][u] = 1 ;
}
for(int i = 1 ; i <= n ; i ++) B[i][n + 1] = B[i][i] = 1 ;
Gauss() ;
Dfs(n , 0) ;
printf("%d\n",Ans) ;
return 0 ;
}

最新文章

  1. elasticsearch GIS空间查询问题解决
  2. CentOS 7 恢复 Windows 启动项
  3. 学习笔记_springmvc返回值、数据写到页面、表单提交、ajax、重定向
  4. 20145212 实验四《Andoid开发基础》
  5. HDU 1082
  6. [译]使用AES 256以达到SSL/TLS安全最大化
  7. [Papers]MHD, $\p_3\pi$, Lebesgue space [Cao-Wu, JDE, 2010]
  8. &lt;你不知道的JavaScript&gt;读书笔记
  9. ASP.NET 2.0服务器控件开发的基本概念(转载)
  10. c++读取ccbi
  11. servlet+ajax+json字符串后台传入,前端解析并把数据循环填入表格实例
  12. Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算(转)
  13. 用eclipse还是myeclipse好
  14. 那些过目不忘的无线端交互设计(DRIBBBLE GIF动态图)
  15. file-API 实现添加图片 预览缩略图(自己学习)
  16. 课堂作业 泛型类-Bag
  17. DbGridEh根据某一个字段的值显示对应底色或字体变化
  18. spring扩展点总结
  19. codeforces723----C. Polycarp at the Radio
  20. 终极解决liunx GUI 无法显示中文的问题。

热门文章

  1. 从零开始写STL—容器—vector
  2. JDBC操作MySQL出现:This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, ...的问题解决
  3. Google Chrome Developer Tools
  4. maven打包插件maven-shade-plugin简单介绍
  5. no matching function transform?
  6. Application特征
  7. ubuntu语言设置成汉语
  8. HttpURL连接远程serverGet和Post方式请求并返回数据
  9. 南阳oj 语言入门 A+B paoblem 题目477 题目844
  10. asp.net mvc 的 视图(view )的模块化开发