题解 | Lights-牛客假日团队赛12K题

Lights

https://ac.nowcoder.com/acm/contest/1081/K

题目描述

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.

输入描述:

* 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 
输出
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.

解答

状压,时间空间都不行,如果每次搜索一半就可以省下很多空间,用map记下每种状态的答案,最后再把两次的答案合并

然而正解是高斯消元解异或方程组,最后搜索自由元

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define ll long long
using namespace std;
const int maxn=40;
const int maxm=600;
int n,m,p,ans=36,fl;
ll f[maxn];
map<ll,int>mx;//每种状态的最小代价 
void dfsl(int x,ll s,int k){
    if(x==m){
        int &t=mx[s];//注意传址的引用 
        if(!t)t=k;
        else if(t>k)t=k;
        return;
    }
    dfsl(x+1,s,k);
    dfsl(x+1,s^f[x],k+1);
}
void dfsr(int x,ll s,int k){
    if(x==m){
        int t=mx[s];
        if(t && t+k-1<ans)ans=t+k-1;
        return;
    }
    dfsr(x+1,s,k);
    dfsr(x+1,s^f[x+m],k+1);
}
int main(){
    scanf("%d%d",&n,&p);
    if(n&1)n++,fl=1;//处理折半搜索 
    m=n/2;
    for(int i=1,x,y;i<=p;i++){
        scanf("%d%d",&x,&y);
        x--,y--;
        f[x]|=1LL<<y,f[y]|=1LL<<x;
    }
    for(int i=0;i<n;i++)f[i]|=1LL<<i;
    dfsl(0,(1LL<<n)-1,1);
    dfsr(0,0,0);
    printf("%d",ans-fl);
}

来源:weixin_30522183 
全部评论
大佬现在还打比赛吗
点赞 回复 分享
发布于 2020-02-19 08:42

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
来个厂收我吧:首先,市场侧求职我不是很懂。 但是,如果hr把这份简历给我,我会觉得求职人不适合做产品经理。 问题点: 1,简历的字体格式不统一,排版不尽如人意 2,重点不突出,建议参考star法则写个人经历 3,印尼官方货币名称为印度尼西亚卢比(IDR),且GMV690000印尼盾换算为305人民币,总成交额不高。 4,右上角的意向职位在发给其他公司时记得删除。 5,你所有的经历都是新媒体运营,但是你要投市场营销岗位,jd和简历不匹配,建议用AI+提示词,参照多个jd改一下经历内容。 修改建议: 1,统一字体(中文:思源黑体或微软雅黑,英文数字:time new romans),在word中通过表格进行排版(b站学) 2,校招个人经历权重:实习经历=创业经历(大创另算)>项目经历>实训经历>校园经历 3,请将项目经历时间顺序改为倒序,最新的放最上方。 4,求职方向不同,简历文字描述侧重点也需要不同。
点赞 评论 收藏
分享
07-15 12:24
重庆大学 运营
坏消息:和好工作擦肩而过
给点吧求求了:怎么可能因为差几秒,估计就是简历更好看婉拒了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务