[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 ...
随机推荐
- codeforces 330b
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 1100 int map[N]; ...
- Memory Ordering in Modern Microprocessors
Linux has supported a large number of SMP systems based on a variety of CPUs since the 2.0 kernel. L ...
- csu - 1566: The Maze Makers (bfs)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1566 题意还是蛮难懂的,至少对于我来说,需要认真读题. 输入矩阵的每一个数字换成2进制后,顺时针围 ...
- Codeforces 628D Magic Numbers
题意: 求在[a,b](a,b不含前导0)中的d−magic数中有多少个是m的倍数. 分析: 计数dp Let's call a number d-magic if digit d appears i ...
- Network-POJ3694(最小公共祖先LCA+Tarjin)
http://poj.org/problem?id=3694 这一题 为什么要找最小祖先呢 当两个节点连到一块的时候 找最小公共节点就相当于找强连通分支 再找最小公共节点的过程中直到找到 这个过 ...
- HDU——2647 Reward
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- 【编程大系】Java资源汇总
1.学习资料: 1)Spring Boot 那些事:https://www.w3cschool.cn/springboot/ 对应的 gitHub代码: https://github.com/Jeff ...
- Cardboard虚拟现实开发初步(二)
Google Cardboard 虚拟现实眼镜开发初步(二) Cardboard SDK for Unity的使用 上一篇文章作为系列的开篇,主要是讲了一些虚拟现实的技术和原理,本篇就会带领大家去看一 ...
- AutoCAD如何设置线宽
一般要求粗实线粗实线0.4,细实线0.2mm. 1 先打开图层特性管理器,新建一个图层,专门放粗实线(我起名叫"我的粗实线",颜色设置为紫色,线宽为0.4mm),此前的乱七八糟的图 ...
- 多个client与一个server端通信的问题
多个client与一个server端通信的问题 上篇博文主要是讲的关于client与server端的通信问题.在上篇博文中当我们仅仅有一个client訪问我们的server时是能够正常执行的,可是当我 ...