题目描述

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.

解题思路

对灯的状态进行分析,每个灯至多会被开一次,因为开两次相当于没开。每个灯的开

与不开会影响它周围的灯,最终的目标为所有灯都被打开,所以可以列出n个方程,

每个方程必须保证异或起来等于1,然后高斯消元求解方程。发现会有自由元,

就搜索求解,如果此时灯是关着的,那么这个自由元必须为1,否则可以不开。

代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. inline int rd(){
  6. int x=0,f=1;char ch=getchar();
  7. while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
  8. while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
  9. return x*f;
  10. }
  11. using namespace std;
  12. const int MAXN = 40;
  13. int n,m,ans=1<<30,light[MAXN];
  14. int a[MAXN][MAXN];
  15. inline void gauss(){
  16. int h=1,l=1;
  17. for(;h<=n && l<=n+1;h++,l++){
  18. int r=h;
  19. for(register int i=h;i<=n;i++) if(a[i][l]) {
  20. r=i;
  21. break;
  22. }
  23. if(r!=h) for(register int i=l;i<=n+1;i++)
  24. swap(a[h][i],a[r][i]);
  25. for(register int i=h+1;i<=n;i++){
  26. if(a[i][l])
  27. for(register int j=l+1;j<=n+1;j++)
  28. a[i][j]^=a[h][j];
  29. a[i][l]=0;
  30. }
  31. }
  32. }
  33. inline void dfs(int x,int tot){
  34. if(tot>ans) return;
  35. if(!x){ans=min(ans,tot);return;}
  36. if(a[x][x]){
  37. light[x]=a[x][n+1];
  38. for(register int i=n;i>x;i--) light[x]^=(light[i]&a[x][i]);
  39. if(light[x]) dfs(x-1,tot+1);
  40. else dfs(x-1,tot);
  41. }
  42. else{
  43. light[x]=1;dfs(x-1,tot+1);
  44. light[x]=0;dfs(x-1,tot);
  45. }
  46. }
  47. int main(){
  48. n=rd();m=rd();
  49. for(register int i=1;i<=m;i++){
  50. int x,y;
  51. x=rd();y=rd();
  52. a[x][y]=a[y][x]=1;
  53. }
  54. for(register int i=1;i<=n;i++) a[i][i]=a[i][n+1]=1;
  55. // for(register int i=1;i<=n;i++){
  56. // for(register int j=1;j<=n+1;j++)
  57. // cout<<a[i][j]<<" ";
  58. // cout<<endl;
  59. // }
  60. gauss();dfs(n,0);printf("%d",ans);
  61. return 0;
  62. }

LUOGU P2962 [USACO09NOV]灯Lights的更多相关文章

  1. luogu P2962 [USACO09NOV]灯Lights 高斯消元

    目录 题目链接 题解 题目链接 luogu P2962 [USACO09NOV]灯Lights 题解 可以折半搜索 map合并 复杂度 2^(n / 2)*logn 高斯消元后得到每个点的翻转状态 爆 ...

  2. P2962 [USACO09NOV]灯Lights 对抗搜索

    \(\color{#0066ff}{题目描述}\) 贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗 ...

  3. 洛谷 P2962 [USACO09NOV]灯Lights

    题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...

  4. [洛谷P2962] [USACO09NOV] 灯Lights

    Description Bessie and the cows were playing games in the barn, but the power was reset and the ligh ...

  5. P2962 [USACO09NOV]灯Lights

    贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望.她希望您能够帮帮她,把所 ...

  6. [USACO09NOV]灯Lights

    题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...

  7. [luoguP2962] [USACO09NOV]灯Lights(高斯消元 + dfs)

    传送门 先进行高斯消元 因为要求最少的开关次数,那么: 对于关键元,我们可以通过带入消元求出, 对于自由元,我们暴力枚举,进行dfs,因为只有开关两种状态,0或1 #include <cmath ...

  8. 【Luogu】P2962灯Lights(折半搜索)

    题目链接 本意是想学高斯消元,然后一顿乱搞之后学到了一个神奇的搜索方式叫做折半搜索. qwq 就是我先dfs前二分之n个点,然后再dfs后二分之n个点. 然后我dfs后二分之n个点的时候判断一下第一次 ...

  9. luogu P1468 派对灯 Party Lamps

    题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都连接到四个按钮: 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄 ...

随机推荐

  1. DEDE采集时自动生成摘要和关键字

    1.修改 include/dedecollection.class.php //自动分析关键字和摘要 preg_match("/<meta[\s]+name=['\"]key ...

  2. HDU - 1560 DNA sequence

    给你最多8个长度不超过5的DNA系列,求一个包含所有系列的最短系列. 迭代加深的经典题.(虽然自己第一次写) 定一个长度搜下去,搜不出答案就加深大搜的限制,然后中间加一些玄学的减枝 //Twenty ...

  3. flask中abort()函数的使用

    一.介绍 #从flask中导入abort from flask import abort abort()函数的作用 可以让开发者在检测到web访问错误时,立即将错误信息返回回去,返回的错误码必须是已知 ...

  4. fatal error U1087: cannot have : and :: dependents for same target Stop.

    转自VC错误:http://www.vcerror.com/?p=72 问题描述: 完成后编译,发现有错误  D:\WinDDK\7600.16385.1\bin\makefile.new(7117) ...

  5. C#实现拍照并且存水印照片

    由于一直在高校工作,就涉及到招生工作,招生时候又要收集学生图像采集,所以就随手写了一个图像采集工具,废话不多说,进入正题. 图像采集需要调用摄像头就行拍照操作,网上查了一下资料,需要引用以下3个dll ...

  6. chown命令使用

    1.原文件为root权限,改为用户所属权限包括文件夹以下的目录这里必须有R chown -R usrname:username /file 2.修改 tmp 目录为可写权限 chmod -R 777 ...

  7. Android开发 处理内存申请失败的报错(Failed to allocate a 38189038 byte allocation with 16777216 free bytes and 20MB until OOM)

    问题原因 当你在操作图片或者其他大量文件数据时会出现:Failed to allocate a 38189038 byte allocation with 16777216 free bytes an ...

  8. linux 系统优化初始化配置

    一.系统优化配置 1.修改yum源  配置国内yum源 阿里云yum源地址 #CentOS 5.x wget -O /etc/yum.repos.d/CentOS-Base.repo http://m ...

  9. server端并发聊天

    mul_server和mul_client实现了客户端发什么消息,服务器端回复什么消息 server_dialog和mul_client实现了客户端与服务器并发通信

  10. 0809NOIP模拟测试赛后总结

    终于有点脸单独建一个随笔写一下考试总结了…… T1一眼组合数学,推了一会儿式子发现恐怕是容斥.突然害怕,于是开始大力dp. 然后骗了70分走人hhh. T2挂了……又读错题了……以为必须相邻,然后就原 ...