洛谷 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

分析:其实就是一道解异或方程组的版题。注意要暴力搜索自由元,我还以为每个自由元都可以直接看作关闭,后面dalao说要枚举自由元,可以前面状态就改了,有可能更优。

代码:

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

const int maxn=50;

int a[maxn][maxn],b[maxn],f[maxn];
int x,y,n,m,i,j,k,ans,cnt;

using namespace std;

void guess()
{
    int i,j,k;
    for (i=1;i<=n;i++)
    {
        k=0;
        for (j=i;j<=n;j++)
        {
            if (a[j][i]) 
            {
                k=j;
                break;
            }
        }
        if (k==0) continue;
        for (j=1;j<=n;j++) swap(a[i][j],a[k][j]);
        swap(b[i],b[k]);
        for (j=1;j<=n;j++)
        {
            if ((a[j][i]==0) || (i==j)) continue;
            for (k=i;k<=n;k++) a[j][k]^=a[i][k];
            b[j]^=b[i];
        }
    }   
}

void dfs(int x,int y)
{
    if (y>ans) return;
    if (x==0) {ans=min(ans,y); return;}
    if (a[x][x])
    {
        f[x]=b[x];
        for (int i=n;i>x;i--) f[x]^=a[x][i]&f[i];
        if (f[x]) dfs(x-1,y+1);
             else dfs(x-1,y);
    }
    else
    {
        f[x]=0;
        dfs(x-1,y);
        f[x]=1;
        dfs(x-1,y+1);
    }
}

int main()
{
    scanf("%d%d",&n,&m);    
    for (i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        a[x][y]=1;
        a[y][x]=1;
    }   
    for (i=1;i<=n;i++)
    {
        a[i][i]=1;
        b[i]=1;
    }
    guess();
    ans=0x3f3f3f3f; 
    dfs(n,0);       
    printf("%d",ans);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值