LUOGU P2962 [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.

解题思路

对灯的状态进行分析,每个灯至多会被开一次,因为开两次相当于没开。每个灯的开
与不开会影响它周围的灯,最终的目标为所有灯都被打开,所以可以列出n个方程,
每个方程必须保证异或起来等于1,然后高斯消元求解方程。发现会有自由元,
就搜索求解,如果此时灯是关着的,那么这个自由元必须为1,否则可以不开。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

inline int rd(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return x*f;
}

using namespace std;
const int MAXN = 40;

int n,m,ans=1<<30,light[MAXN];
int a[MAXN][MAXN];

inline void gauss(){
    int h=1,l=1;
    for(;h<=n && l<=n+1;h++,l++){
        int r=h;
        for(register int i=h;i<=n;i++) if(a[i][l]) {
            r=i;
            break;
        }
        if(r!=h) for(register int i=l;i<=n+1;i++)
            swap(a[h][i],a[r][i]);
        for(register int i=h+1;i<=n;i++){
            if(a[i][l])
            for(register int j=l+1;j<=n+1;j++)
                a[i][j]^=a[h][j];
            a[i][l]=0;
        }
    }
}

inline void dfs(int x,int tot){
    if(tot>ans) return;
    if(!x){ans=min(ans,tot);return;}
    if(a[x][x]){
        light[x]=a[x][n+1];
        for(register int i=n;i>x;i--) light[x]^=(light[i]&a[x][i]);
        if(light[x]) dfs(x-1,tot+1);
        else dfs(x-1,tot);
    }
    else{
        light[x]=1;dfs(x-1,tot+1);
        light[x]=0;dfs(x-1,tot);
    }
}

int main(){
    n=rd();m=rd();
    for(register int i=1;i<=m;i++){
        int x,y;
        x=rd();y=rd();
        a[x][y]=a[y][x]=1;
    }
    for(register int i=1;i<=n;i++) a[i][i]=a[i][n+1]=1;
//  for(register int i=1;i<=n;i++){
//      for(register int j=1;j<=n+1;j++)
//          cout<<a[i][j]<<" ";
//      cout<<endl; 
//  }
    gauss();dfs(n,0);printf("%d",ans);

    return 0;
}
posted @ 2018-07-02 11:08  Monster_Qi  阅读(160)  评论(0编辑  收藏  举报