[USACO09NOV]灯Lights 题解
题目地址:洛谷:【P2962】[USACO09NOV]灯Lights – 洛谷、BZOJ:Problem 1770. — [Usaco2009 Nov]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.
题解
考虑一个灯亮的时候,它和与它有边相连的灯的开关次数一定是奇数,那么我们可以把这个开关次数设成未知数,每个灯列一个异或方程,就得到了一个n元异或方程组。这个可以用高斯消元解决。然而如果有自由元的情况我们需要看一下怎么来安排它比较优,因此就用DFS来计算答案。
代码
// Code by KSkun, 2018/3
#include <cstdio>
#include <algorithm>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXN = 105;
int n, m;
bool mat[MAXN][MAXN];
inline void gauss() {
for(int i = 1; i <= n; i++) {
int r = i;
for(int j = i + 1; j <= n; j++) {
if(mat[j][i]) {
r = j;
break;
}
}
if(r != i) {
for(int j = 1; j <= n + 1; j++) std::swap(mat[i][j], mat[r][j]);
}
for(int j = i + 1; j <= n; j++) {
if(mat[j][i]) {
for(int k = 1; k <= n + 1; k++) mat[j][k] ^= mat[i][k];
}
}
}
}
int ans = 1e9;
bool res[MAXN];
inline void dfs(int x, int tot) {
if(tot > ans) return;
if(!x) {
ans = std::min(ans, tot);
return;
}
if(mat[x][x]) {
res[x] = mat[x][n + 1];
for(int i = n; i > x; i--) {
if(mat[x][i]) res[x] ^= res[i];
}
if(res[x]) dfs(x - 1, tot + 1); else dfs(x - 1, tot);
} else {
res[x] = 0; dfs(x - 1, tot);
res[x] = 1; dfs(x - 1, tot + 1);
}
}
int u, v;
int main() {
n = readint(); m = readint();
while(m--) {
u = readint(); v = readint();
mat[u][v] = mat[v][u] = 1;
}
for(int i = 1; i <= n; i++) {
mat[i][n + 1] = 1;
mat[i][i] = 1;
}
gauss();
dfs(n, 0);
printf("%d", ans);
return 0;
}