\(\color{#0066ff}{题目描述}\)

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

\(\color{#0066ff}{输入格式}\)

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

\(\color{#0066ff}{输出格式}\)

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

\(\color{#0066ff}{输入样例}\)

  1. 5 6
  2. 1 2
  3. 1 3
  4. 4 2
  5. 3 4
  6. 2 5
  7. 5 3

\(\color{#0066ff}{输出样例}\)

  1. 3

\(\color{#0066ff}{题解}\)

看n的范围,显然可以对抗搜索

用\(O(2^n)\)枚举出前半部分的点选或不选,并记录到达状态的最小步数(用map)

再搜后半部分点选或不选,只要能跟刚刚的拼上,就更新答案

  1. #include<cstdio>
  2. #include<queue>
  3. #include<vector>
  4. #include<iostream>
  5. #include<cstring>
  6. #include<algorithm>
  7. #include<cctype>
  8. #include<cmath>
  9. #include<map>
  10. #define _ 0
  11. #define LL long long
  12. #define Space putchar(' ')
  13. #define Enter putchar('\n')
  14. #define fuu(x,y,z) for(int x=(y),x##end=z;x<=x##end;x++)
  15. #define fu(x,y,z) for(int x=(y),x##end=z;x<x##end;x++)
  16. #define fdd(x,y,z) for(int x=(y),x##end=z;x>=x##end;x--)
  17. #define fd(x,y,z) for(int x=(y),x##end=z;x>x##end;x--)
  18. #define mem(x,y) memset(x,y,sizeof(x))
  19. #ifndef olinr
  20. inline char getc()
  21. {
  22. static char buf[100001],*p1=buf,*p2=buf;
  23. return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100001,stdin),p1==p2)? EOF:*p1++;
  24. }
  25. #else
  26. #define getc() getchar()
  27. #endif
  28. template<typename T>inline void in(T &x)
  29. {
  30. int f=1; char ch; x=0;
  31. while(!isdigit(ch=getc()))(ch=='-')&&(f=-f);
  32. while(isdigit(ch)) x=x*10+(ch^48),ch=getc();
  33. x*=f;
  34. }
  35. int n,m;
  36. std::map<LL,int> mp;
  37. LL zt[55];
  38. int mid;
  39. int ans=0x7fffffff;
  40. inline void dfs1(int now,LL z,int step)
  41. {
  42. if(now==mid+1)
  43. {
  44. if(!mp.count(z)) mp[z]=step;
  45. else mp[z]=std::min(mp[z],step);
  46. return;
  47. }
  48. dfs1(now+1,z^zt[now],step+1);
  49. dfs1(now+1,z,step);
  50. }
  51. inline void dfs2(int now,LL z,int step)
  52. {
  53. if(now==n+1)
  54. {
  55. if(mp.count(z)) ans=std::min(ans,step+mp[z]);
  56. return;
  57. }
  58. dfs2(now+1,z^zt[now],step+1);
  59. dfs2(now+1,z,step);
  60. }
  61. int main()
  62. {
  63. in(n),in(m);
  64. int x,y;
  65. fuu(i,1,m) in(x),in(y),zt[x]|=1LL<<(y-1),zt[y]|=1LL<<(x-1);
  66. fuu(i,1,n) zt[i]|=(1<<(i-1));
  67. mid=(1+n)>>1;
  68. dfs1(1,(1LL<<n)-1,0);
  69. dfs2(mid+1,0LL,0);
  70. printf("%d",ans);
  71. return ~~(0^_^0);
  72. }

P2962 [USACO09NOV]灯Lights 对抗搜索的更多相关文章

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

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

  2. 洛谷 P2962 [USACO09NOV]灯Lights

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

  3. LUOGU 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. BZOJ 3106: [cqoi2013]棋盘游戏(对抗搜索)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3106 对抗搜索,f[x][y][a][b][c][d]表示当前谁走,走了几步,及位置. (因为 ...

  9. BZOJ.5248.[九省联考2018]一双木棋chess(对抗搜索 记忆化)

    BZOJ 洛谷P4363 [Update] 19.2.9 重做了遍,感觉之前写的有点扯= = 首先棋子的放置情况是阶梯状的. 其次,无论已经放棋子的格子上哪些是黑棋子哪些是白棋子,之前得分如何,两人在 ...

随机推荐

  1. MFRC522模块开发笔记

    Write_to_Card(-)和Read_from_Card(-)可谓是所有函数的终点,而SPIWriteByte(-)则是最底层对MFRC522模块进行操作的函数,所有函数都是为了Write_to ...

  2. java ----获取路径的各种方法(总结)

    Java Web开发中路径问题小结 (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 那么针对这个站点的几个基本概念表述如下: 1. web站点的根目录:http: ...

  3. phonegap中使用自带浏览器打开链接

    <center><a id="ssl2" href="#" onclick="window.open('http://127.0.0 ...

  4. Python代码规范总结

    1.缩进问题: Tip:用4个空格来缩进代码 不要用Tab键或者是Tab和空格混用, vim用户可以将tab键设置为4个空格的长度.要么选择垂直对齐换行的元素, 或者是使用4空格悬挂式缩进(第一行没有 ...

  5. Ubuntu 开启telnet、ftp服务

    Telnet 这里我们就来对Ubuntu Linux telnet的安装设置进行一下讲解. 1. sudo apt-get install xinetd telnetd 2. Ubuntu Linux ...

  6. 偏好设置(Preference)

    一.Preference简介 (1)偏好设置是专门用来保存应用程序的配置信息的, 一般情况不要在偏好设置中保存其他数据.如果利用系统的偏好设置来存储数据, 默认就是存储在Library/Prefere ...

  7. springmvc 注解扫描失败的可能原因

    情况是这样的:web工程采用了ssm框架,dao和service都是通过annotation方式注入的,工程运行正常.后来把service和dao打成jar放在工程的lib目录下,问题来了,配置没改动 ...

  8. windows 7 系统装机优化

    A:系统设置 1.控制面板\系统和安全\Windows Update\更改设置  把系统升级以及提示关闭      控制面板\系统和安全\Windows 防火墙\自定义设置 把专用网络和公共网络的防火 ...

  9. ROS Learning-014 learning_tf(编程) 坐标系变换(tf)广播员 (Python版)

    ROS Indigo learning_tf-01 坐标系变换(tf)广播员 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu ...

  10. JavaPersistenceWithHibernate第二版笔记-第七章-004Mapping a map(@MapKeyEnumerated 、 @MapKeyTemporal、@MapKeyColumn)

    一.结构 二.代码 1. package org.jpwh.model.collections.mapofstrings; import org.jpwh.model.Constants; impor ...