题目描述

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盏熄灭的灯和m条边,当按一个灯的开关时,这个灯连同和它相连的灯的状态都会改变,

求最少按几次,灯全是打开的。

题解:

40分dfs 每盏灯的状态是按还是不按开关

错因:当按这盏灯的开关时,应该是状态改变而不是打开。

只改变了和这盏灯相邻的灯的状态,没有改变这盏灯的状态。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int n,m,sumedge,ans,js;
int head[],d[]; struct Edge{
int x,y,nxt;
Edge(int x=,int y=,int nxt=):
x(x),y(y),nxt(nxt){}
}edge[]; void add(int x,int y){
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
} void dfs(int x,int ste){
bool can=true;
for(int i=;i<=n;i++)
if(!d[i]){
can=false;
break;
}
if(can){
ans=min(ans,ste);return;
}
if(x==n+)return;
d[x]=^d[x];
for(int i=head[x];i;i=edge[i].nxt){
int v=edge[i].y;
d[v]=^d[v];
}
dfs(x+,ste+);
d[x]=^d[x];
for(int i=head[x];i;i=edge[i].nxt){
int v=edge[i].y;
d[v]=^d[v];
}
dfs(x+,ste);
} int main(){
scanf("%d%d",&n,&m);
for(register int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
ans=0x7fffffff;
dfs(,);
cout<<ans<<endl;
return ;
}

昨天听老徐讲的折半搜索..将hzwer的代码注释了一下....

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define inf 1000000000
#define ll long long
using namespace std; inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} bool flag;
int n,m,cnt,mn=inf;
int a[];
ll ed,p[],bin[];
map<ll,int> b; void dfs(int x,ll now,int used)//x表示搜的第几盏灯,now表示当前的状态,used表示按了几次开关。
{ //假如now 的二进制表示为 1 1 0 0 1,这里的1表示这盏灯开着。
//而之前的p数组的二进制串的1表示这盏灯的状态改变,哪些灯受影响。
if(x==cnt+)
{
if(now==ed)mn=min(used,mn);//如果当前状态灯都开着,取最小的步数。
if(!flag)//flag表示第几次搜索,这里是一第一次。
{
int t=b[now];//用stl里的map,来表示之前是否到达过now状态,并记录步数
if(!t||t>used)b[now]=used;//如果之前没有出现过now状态,或者之前出现过,但是之前达到now状
//态的步数比当前的步数大,更新。
}
else //第二次搜索
{
int t=b[ed-now];//与第一次搜索的状态进行合并。
if(!t)return;//如果在第一次中找不到能和该状态合并的状态,就返回。
mn=min(t+used,mn);//能找到,用第一次搜索的步数和第二次搜索的步数的和更新答案。
}
return;
}
dfs(x+,now,used); //不改变当前灯的状态。
dfs(x+,now^p[x],used+);//改变当前灯的状态。
} int main()
{
bin[]=;for(int i=;i<;i++)bin[i]=bin[i-]<<;//bin[i]为2的i-1次方
n=read();m=read();
ed=bin[n+]-;//这个ed的作用为判断状态,这个ed的二进制为1 1 1 1 ,表示灯都开着
//当有三盏灯时 ———,当为1 1 1 时用二进制表示是7,
//当有两盏灯时,——,当为1 1 ,用二进制表示是3,就是2^(灯的数量)-1
for(int i=;i<=m;i++)
{
int a=read(),b=read();
p[a]+=bin[b];p[b]+=bin[a];
//p[i]表示第i盏等状态改变时,受影响的灯。
//如 0 1 1 0 0,这个二进制串其中的1表示,第i盏灯状态改变受影响的灯。
//为什么是+,而不是赋值呢,是因为如果当前二进制串为0 0 1 0,1表示改变a灯状态受影响的灯。
//当又有灯与a相连时,如c灯 0 1 0 0 ,相加 后的串 0 1 1 0,表示与a的状态有关的灯。
}
for(int i=;i<=n;i++)p[i]+=bin[i];//表示第i盏灯会影响自己。
cnt=n/;dfs(,,);//折半搜索。
flag=;//表示搜的前一半还是后一半。
cnt=n;dfs(n/+,,);
printf("%d\n",mn);
return ;
}

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

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

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

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

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

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

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

  4. LUOGU P2962 [USACO09NOV]灯Lights

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

  5. P2962 [USACO09NOV]灯Lights

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

  6. 洛谷P2845-Switching on the Lights 开关灯

    Problem 洛谷P2845-Switching on the Lights 开关灯 Accept: 154    Submit: 499Time Limit: 1000 mSec    Memor ...

  7. 洛谷 1938 [USACO09NOV]找工就业Job Hunt

    洛谷 1938  [USACO09NOV]找工就业Job Hunt 题目描述 Bessie is running out of money and is searching for jobs. Far ...

  8. [USACO09NOV]灯Lights

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

  9. 洛谷 P1876 开灯(思维,枚举,规律题)

    P1876 开灯 题目背景 该题的题目是不是感到很眼熟呢? 事实上,如果你懂的方法,该题的代码简直不能再短. 但是如果你不懂得呢?那...(自己去想) 题目描述 首先所有的灯都是关的(注意是关!),编 ...

随机推荐

  1. slam cartographer 学习

    https://github.com/slam4code                   感谢大牛的分享

  2. 手把手实现andriod应用增量升级

    近期研究了android应用增量升级的应用.当中用到了android NDK编程,先说下为什么要使用增量升级.当我们的应用达到一定大小的时候,比方眼下有30M.假设新版本号35M仅仅是添加了几个功能, ...

  3. 【7.1.1】ELK集群搭建 之 ES集群

    写在前边 昨天晚上就已经完成这篇博客了,就是在测试这块是否正常跑起来,晚上没搞完,上班前把电脑关机带着,结果没保存!基本上昨天写的东西都丢了,好在博客园的图片url还在. 为了让大家都轻松些,我轻松写 ...

  4. Spring利用propertyConfigurer类 读取.property数据库配置文件

    (1).基本的使用方法是: <bean id="propertyConfigurerForAnalysis" class="org.springframework. ...

  5. intellij 开发webservice

    最近项目中有用到WebService,于是就研究了一下,但是关于intellij 开发 WebService 的文章极少,要不就是多年以前,于是研究一下,写这篇博文.纯属记录,分享,中间有不对的地方, ...

  6. WebStorm 常用功能

    WebStorm 常用功能的使用技巧分享 WebStorm 是 JetBrain 公司开发的一款 JavaScript IDE,使用非常方便,可以使编写代码过程更加流畅. 本文在这里分享一些常用功能的 ...

  7. Java带标签的break 和带标签的continue

    最开始没有学习java 学习的是C语言然后工作开始用java,但当时并没有仔细看过java的书籍,也是大致一翻就看跟C语言很像,了解了基本语法就没有深究了,今天看书开始发现之前没有了解过的语法 带标签 ...

  8. Oracle 10g ORA-12154: TNS: could not resolve the connect identifier specified 问题解决! 我同事遇到的问题。 username/

    Oracle 10g ORA-12154: TNS: could not resolve the connect identifier specified 问题解决! 我同事遇到的问题. userna ...

  9. libEasyPlayer RTSP windows播放器SDK API接口设计说明

    概述 libEasyPlayer实现对RTSP直播流进行实时采集和解码显示,稳定,高效,低延时:解码可采用intel硬件解码和软件解码两种方式,能实时进行录像和快照抓图,OSD叠加等功能. API接口 ...

  10. sql中decode()重要函数使用

    decode()函数简介: 主要作用:将查询结果翻译成其他值(即以其他形式表现出来,以下举例说明): 使用方法: Select decode(columnname,值1,翻译值1,值2,翻译值2,.. ...