[洛谷P2962] [USACO09NOV] 灯Lights
Description###
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)条很神奇的无向边,每条边连接两盏灯。 每盏灯上面都带有一个开关。当按下某一盏灯的开关的时候,这盏灯本身,还有所有有边连向这盏灯的灯的状态都会被改变。状态改变指的是:当一盏灯是开著的时候,这盏灯被关掉;当一盏灯是关著的时候,这盏灯被打开。 问最少要按下多少个开关,才能把所有的灯都给重新打开。 数据保证至少有一种按开关的方案,使得所有的灯都被重新打开。
Input###
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.
Output###
Line 1: A single integer representing the minimum number of switches that need to be flipped in order to turn on all the lights.
Sample Input###
5 6
1 2
1 3
4 2
3 4
2 5
5 3
Sample Output###
3
HINT###
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.
想法##
异或方程组
首先,显然每个灯最多只会被开一次。那么设x[i]表示第i盏灯是否被开
那可以列n个形如 (map[1][i] * x[1])^(map[2][i] * x[2])…(map[n][i] * x[n])=1 的异或方程,表示这盏灯最终的状态为开
然后先用高斯消元解一下,但可能有些x[]前面的系数为0
在遇到这种情况时需要对于这个x是0还是1进行dfs
代码##
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 40;
typedef int Mat[N][N];
int n,m;
void gauss(Mat A){
for(int i=1;i<=n;i++){
int r=i;
for(int j=i+1;j<=n;j++)
if(A[j][i]>A[r][i]) r=j;
for(int j=i;j<=n+1;j++) swap(A[i][j],A[r][j]);
if(!A[i][i]) continue;
for(int j=i+1;j<=n;j++)
if(A[j][i])
for(int k=i;k<=n+1;k++) A[j][k]^=A[i][k];
}
}
Mat a;
int ans,val[N];
void dfs(int u,int c){
if(c>ans) return;
if(u==0) {
ans=c;
return;
}
if(a[u][u]){
val[u]=a[u][n+1];
for(int j=u+1;j<=n;j++)
if(a[u][j]) val[u]^=val[j];
dfs(u-1,c+val[u]);
}
else{
val[u]=1;
dfs(u-1,c+1);
val[u]=0;
dfs(u-1,c);
}
}
int main()
{
int x,y;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
a[x][y]=a[y][x]=1;
}
for(int i=1;i<=n;i++) a[i][n+1]=a[i][i]=1;
gauss(a);
ans=n+1;
dfs(n,0);
printf("%d",ans);
return 0;
}
[洛谷P2962] [USACO09NOV] 灯Lights的更多相关文章
- 洛谷 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 高斯消元
目录 题目链接 题解 题目链接 luogu P2962 [USACO09NOV]灯Lights 题解 可以折半搜索 map合并 复杂度 2^(n / 2)*logn 高斯消元后得到每个点的翻转状态 爆 ...
- P2962 [USACO09NOV]灯Lights 对抗搜索
\(\color{#0066ff}{题目描述}\) 贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗 ...
- 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
贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望.她希望您能够帮帮她,把所 ...
- 洛谷P2845-Switching on the Lights 开关灯
Problem 洛谷P2845-Switching on the Lights 开关灯 Accept: 154 Submit: 499Time Limit: 1000 mSec Memor ...
- 洛谷 1938 [USACO09NOV]找工就业Job Hunt
洛谷 1938 [USACO09NOV]找工就业Job Hunt 题目描述 Bessie is running out of money and is searching for jobs. Far ...
- [USACO09NOV]灯Lights
题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...
- 洛谷 P1876 开灯(思维,枚举,规律题)
P1876 开灯 题目背景 该题的题目是不是感到很眼熟呢? 事实上,如果你懂的方法,该题的代码简直不能再短. 但是如果你不懂得呢?那...(自己去想) 题目描述 首先所有的灯都是关的(注意是关!),编 ...
随机推荐
- axios发送POST时请求两次,第一次为OPTIONS
出现问题: 发送POST请求时浏览器产生两次请求,第一次为OPTIONS,第二次是真正的POST请求,后台接收不到参数. 查找原因: 非GET请求,会先发送OPTIONS进行预检(预检请求每次运行只发 ...
- 1024程序员节!(JAVA Code)
点我:传送门 程序员节快乐~ 水水题 A import java.util.*; import java.io.*; public class Main { public static void m ...
- 2018-8-10-WPF-修改图片颜色
title author date CreateTime categories WPF 修改图片颜色 lindexi 2018-08-10 19:16:53 +0800 2018-07-03 15:4 ...
- 2019前端学习路线心得-黑马程序员pink老师
在规划之前先给大家分享几点心得哈: 1. 学习,特别是在线学习,是非常辛苦的事情,为了少走弯路, 所以一定要系统学习,多借鉴与前辈们总结出来的经验. 2. 不要相信任何说 一周掌握 css, 一周学完 ...
- 【转】Elasticsearch学习笔记
一.常用术语 索引(Index).类型(Type).文档(Document) 索引Index是含有相同属性的文档集合.索引在ES中是通过一个名字来识别的,且必须是英文字母小写,且不含中划线(-):可类 ...
- 使用BFD检测EBGP邻居
在广域网BGP环境中,通常使能BFD以快速实现链路故障后的路由的主动收敛. BFD使用UDP在链路上进行双向检测,BFD有Echo mode和asynchronous mode两种模式,默认为Echo ...
- read、write 与recv、send区别 gethostname
recv相对于read有什么区别呢? 其实它跟read函数功能一样,都可以从套接口缓冲区sockfd中取数据到buf,但是recv仅仅只能够用于套接口IO,并不能用于文件IO以及其它的IO,而read ...
- docker mysql配置挂载到卷
docker--将mysql配置挂载到卷 1.首先在根目录创建两个文件夹,其中config文件夹中创建my.cnf配置文件.data文件夹存放数据文件,一定要为空. /docker/mysql/con ...
- 【学习笔鸡】整体二分(P2617 Dynamic Rankings)
[学习笔鸡]整体二分(P2617 Dynamic Rankings) 可以解决一些需要树套树才能解决的问题,但要求询问可以离线. 首先要找到一个具有可二分性的东西,比如区间\(k\)大,就很具有二分性 ...
- 洛谷$P2469\ [SDOI2010]$ 星际竞速 网络流
正解:网络流 解题报告: 传送门$QwQ$ 题目好长昂,,,大概概括下就说有$m$条单向边,$n$个点,每条边有一条边权,每个点有一个点权,然后问每个点都要到达一遍的最小代价是多少$QwQ$? 发现有 ...