[USACO09NOV]灯Lights
题目描述
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 ;
}
[USACO09NOV]灯Lights的更多相关文章
- luogu P2962 [USACO09NOV]灯Lights 高斯消元
目录 题目链接 题解 题目链接 luogu P2962 [USACO09NOV]灯Lights 题解 可以折半搜索 map合并 复杂度 2^(n / 2)*logn 高斯消元后得到每个点的翻转状态 爆 ...
- P2962 [USACO09NOV]灯Lights 对抗搜索
\(\color{#0066ff}{题目描述}\) 贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗 ...
- 洛谷 P2962 [USACO09NOV]灯Lights
题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...
- LUOGU P2962 [USACO09NOV]灯Lights
题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...
- [洛谷P2962] [USACO09NOV] 灯Lights
Description Bessie and the cows were playing games in the barn, but the power was reset and the ligh ...
- P2962 [USACO09NOV]灯Lights
贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望.她希望您能够帮帮她,把所 ...
- [luoguP2962] [USACO09NOV]灯Lights(高斯消元 + dfs)
传送门 先进行高斯消元 因为要求最少的开关次数,那么: 对于关键元,我们可以通过带入消元求出, 对于自由元,我们暴力枚举,进行dfs,因为只有开关两种状态,0或1 #include <cmath ...
- 【Luogu】P2962灯Lights(折半搜索)
题目链接 本意是想学高斯消元,然后一顿乱搞之后学到了一个神奇的搜索方式叫做折半搜索. qwq 就是我先dfs前二分之n个点,然后再dfs后二分之n个点. 然后我dfs后二分之n个点的时候判断一下第一次 ...
- meet in the middle 折半搜索 刷题记录
复杂度分析 假设本来是n层,本来复杂度是O(2^n),如果meet in middle那就是n/2层,那复杂度变为O( 2^(n/2) ),跟原来的复杂度相比就相当于开了个方 比如如果n=40那爆搜2 ...
随机推荐
- 289. Game of Live
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- 创建Django项目(二)——数据库配置
2013-08-05 20:53:44| 1.数据库配置 举例是用MySQL数据库,首先在settings文件中做配置,如下: DATABASES = { ' ...
- lambda简单记录
lambda表达式对集合的一些操作,持续记录一下新的用法 List<Integer> list = new ArrayList<>(); list.add(1); list.a ...
- 洛谷 P1404 平均数
P1404 平均数 题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 输入输出格式 输入格式: N+1行, 第一行两个整数n和m 接下来n ...
- SHARP AR-2048D/2348D
http://www.sharp.cn/printer/AR-2048D%7C2348D/support/download.html
- laravel event
事件监听 方法一: web.php Event::listen('eloquent.created: App\post',function(){ dump('A post was created'); ...
- 类的operator new与operator delete的重载【转】
http://www.cnblogs.com/luxiaoxun/archive/2012/08/11/2633423.html 为什么有必要写自己的operator new和operator del ...
- HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)
Turn the pokers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 我和nupt集训队的故事
纯水文,如有不适请ctrl+w撤离 亚洲赛刚结束.看了不少巨巨的退役贴以及岛娘在知乎上的那篇感天动地的人生经历.多少有点夜深忽梦少年事的错觉.作为一个两年前就打出gg的高龄选手,之后又强行以1次队员和 ...
- Linux系统编程_6_进程环境(C程序典型的存储空间)
1.八种结束Linux进程的方法: 五种正常终止方式: main函数返回: 调用exit: 调用_exit或_Exit 最后一个线程从其启动例程返回 最后一个线程调用pthread_exit 三种异常 ...