【Luogu】P2962灯Lights(折半搜索)

  题目链接

  本意是想学高斯消元,然后一顿乱搞之后学到了一个神奇的搜索方式叫做折半搜索。

  qwq

  就是我先dfs前二分之n个点,然后再dfs后二分之n个点。

  然后我dfs后二分之n个点的时候判断一下第一次dfs有没有搜到互补的状态(就是当前状态能不能跟之前搜到过的一个状态异或起来变成(1<<n)-1,即所有灯都开着)

  如果能就看看能不能更新答案。

  然后快的飞起qwq

  

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<map>
#define maxn 37
#define maxs (1<<14)
using namespace std;
map<long long,int> sta,stb;
inline long long read(){
    long long num=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-')    f=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        num=num*10+ch-'0';
        ch=getchar();
    }
    return num*f;
}

int n,m;
long long chan[maxn];
long long Max;
long long cnt;
int ans=0x7fffffff;

void dfs(int x,long long state,int num){
    cnt++;
    if(sta.count(state))    sta[state]=min(sta[state],num);
    else                    sta[state]=num;
    if(x==(n>>1)+1)    return;
    long long now=state^chan[x];
    dfs(x+1,state,num);
    dfs(x+1,now,num+1);
}

void dfs2(int x,long long state,int num){
    cnt++;
    if(stb.count(state))    stb[state]=min(stb[state],num);
    else                    stb[state]=num;
    if(sta.count(Max^state))    ans=min(ans,sta[Max^state]+stb[state]);
    if(x==n+1)    return;
    long long now=state^chan[x];
    dfs2(x+1,state,num);
    dfs2(x+1,now,num+1);
}

int main(){
    n=read(),m=read();
    Max=(1LL<<n)-1;
    for(int i=1;i<=m;++i){
        long long x=read(),y=read();
        chan[x]|=(1LL<<(y-1));
        chan[y]|=(1LL<<(x-1));
    }
    for(long long i=1;i<=n;++i)    chan[i]|=(1LL<<(i-1));
    dfs(1,0,0);
    dfs2((n>>1)+1,0,0);
    printf("%d",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/cellular-automaton/p/8242580.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值