[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 ...
随机推荐
- msp430入门编程01
msp430单片机最小系统 msp430入门学习 msp430入门编程
- hosts.allow和hosts.deny文件
之前想通过外部主机访问自己主机上的VMWare虚拟机,使用了VMWare的NAT端口映射,经过一番尝试,算是成功了,总结一下. VMWare NAT端口映射就可以将虚拟机上的服务映射成自己主机上的端口 ...
- UVA 11400_ Lighting System Design
题意: 给定一系列灯泡的额定功率,电源价钱,一个灯泡的价格以及系统所需该种灯泡的数量.已知流过灯泡的电流相等,所以为省钱可以将电压小的灯泡换成电压大的灯泡,但是不能换成电压更小的灯泡,问最少要花多少钱 ...
- Linux下查看硬盘UUID和修改硬盘UUID(转)
查看硬盘UUID: 1. ls -l /dev/disk/by-uuid 2. blkid /dev/sda5 修改硬盘UUID: 1.新建和改变分区的UUID sudo uuidgen | xarg ...
- java开发中涉及到的调优
JVM内存的调优 默认的Java虚拟机的大小比较小,在对大数据进行处理时java就会报错:java.lang.OutOfMemoryError. 1. Heap设定与垃圾回收Java Heap分为3个 ...
- ADSL和ITV
1.ADSL和ITV两者占用的是不同的虚通道,也就是使用不同的VLAN: 2.的确上通过不同的VPI/VCI来区分ADSL和ITV在不同通道,但不会互不影响的,因为使用的还是同一条线路的宽带速度: 3 ...
- pydevd 一次trouble shooting
只是一次小的trouble shooting. 關於python的遠程調試功能.但是由於思路混亂.浪費了許多時間,記錄一下整個過程.作爲改進的參考. 问题背景: 我之前一直在ubuntu上用pycha ...
- Android基础新手教程——4.3.1 BroadcastReceiver牛刀小试
Android基础新手教程--4.3.1 BroadcastReceiver牛刀小试 标签(空格分隔): Android基础新手教程 本节引言 本节我们将来学习Android四大组件中的第三个:Bro ...
- 1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise
题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chine ...
- web 开发之js---理解并解决IE的内存泄漏方式
程序当中任何编程内存操作不当都会导致内存泄漏 http://wenku.baidu.com/link?url=8ba4UIn1aaevxTagH-F4vID79-bAfxdcLdeujGFn7PBnv ...