[USACO09NOV]灯Lights
来源:互联网 发布:sql语言的特点 编辑:程序博客网 时间:2024/06/10 16:14
题目描述
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.
折半搜索(Meet in the Middle)
对于该问题,我们可以想到使用状态压缩进行搜索,时间复杂度为
在具体记录中间状态时,可以直接使用STL set, 用位压的方法足够用long long来记录每一盏灯的亮灭情况。
/*112ms , 9.82MB*/#include<bits/stdc++.h>using namespace std;typedef long long ll;const int N = 40;const int INF = 0x3f3f3f3f;ll e[N], bin[N], ed;map<ll, int> mp;int n, m, mt = INF, cnt;bool flag=0;void dfs(int x, ll now, int used){ if(x == cnt+1){ if(now == ed) mt = min(mt, used); if(!flag){ int t = mp[now]; if(!t||t>used) mp[now] = used; } else{ int t = mp[ed-now]; if(!t) return; mt = min(mt, t+used); } return; } dfs(x+1, now, used); dfs(x+1, now^e[x], used+1);}int main(){ for(int i = 1; i < N; i++) bin[i] = (ll)1 << (i-1); scanf("%d%d", &n, &m); ed = bin[n+1]-1; for(int i = 1; i <= n; i++) e[i]+=bin[i];//用e[i]表示该灯与其他灯的联系, //与第i盏灯有关系,则第i-1位为1 for(int i = 1; i <= m; i++){ int u, v; scanf("%d%d", &u, &v); e[u]+=bin[v]; e[v]+=bin[u]; } cnt = n/2; dfs(1, 0, 0); flag = 1; cnt = n;dfs(n/2+1, 0, 0); printf("%d\n", mt); return 0;}
高斯消元解异或方程组+DFS
对于开关灯问题,我们很自然地想到高斯消元。当我们对原方程进行高斯消元后。我们发现如果该变量是非自由变元,则我们对着盏灯进行的操作就是确定的,可以通过之前的操作情况递推出来,否则该变量是自由变元,那么意味着我们有两种选择——操作或不操作。这时我们不如对该元进行深搜。
/*0ms , 2.12MB*/#include<bits/stdc++.h>using namespace std;const int N = 40;int f[N][N], x[N];int n, m, ans = INT_MAX;void Gauss(){ for(int i = 1; i <= n; i++){ int j = i; while(j <= n && !f[j][i]) j++; if(j > n) continue; if(i != j) for(int k = 1; k <= n+1; k++) swap(f[i][k], f[j][k]); for(int j = i+1; j <= n; j++) if(f[j][i]) for(int k = 1; k <= n+1; k++) f[j][k]^=f[i][k]; }}void dfs(int now, int cnt){ if(cnt >= ans) return; if(!now){ans = min(ans, cnt); return;} if(f[now][now]){ x[now] = f[now][n+1];//x数组保存当前的操作情况 for(int i = now+1; i <=n; i++) x[now]^=x[i]&f[now][i]; if(x[now]) dfs(now-1, cnt+1); else dfs(now-1, cnt); } else{ x[now] = 0; dfs(now-1, cnt); x[now] = 1; dfs(now-1, cnt+1); }}int main(){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) f[i][i] = f[i][n+1] = 1;//邻接矩阵保存 for(int i = 1; i <= m; i++){ int u, v; scanf("%d%d", &u, &v); f[u][v] = f[v][u] = 1; } Gauss(); dfs(n, 0); printf("%d\n", ans); return 0;}
- [USACO09NOV]灯Lights
- [Usaco09Nov] 找工作
- Lights
- Lights
- [USACO09NOV]Job Hunt
- BZOJ1770: [Usaco2009 Nov]lights 灯
- bzoj1770: [Usaco2009 Nov]lights 灯
- BZOJ 1770 USACO 2009 Nov Lights 灯 高斯消元异或方程组
- Android Lights
- Unity3D Lights
- Multiple Lights
- hdu5820 Lights
- unity - lights
- 洛谷 P2964 [USACO09NOV] 硬币的游戏A Coin Game
- SGU 103 Traffic Lights
- poj 1158 TRAFFIC LIGHTS
- Android Lights笔记
- C - Extended Lights Out
- 【安全牛学习笔记】漏洞扫描
- 5、玩转树莓派音频——打造便携式合成器
- 量化分析(7)——移动均线、macd线
- 重写mtcnn遇到的python及tensorflow问题
- VirtualBox内Linux系统怎样与Windows共享文件夹(Ubuntu)
- [USACO09NOV]灯Lights
- leetcode问题解答: 22. Generate Parentheses
- 【安全牛学习笔记】DNS信息收集
- BSS段、数据段、代码段、堆与栈
- 绘图和可视化(seaborn)
- 编程语言概览
- IE9下不打开F12调试工具无法打开页面的问题解决
- C语言的函数
- [COGS2652]秘术「天文密葬法」-长链剖分-01分数规划