HDU 1814 Peaceful Commission (最小字典序 2-SAT)

思路:

因为要解决最小最小字典序的问题,所以经典的2-SAT解决方法就没用了。。。只能暴力dfs。。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;

const int maxn = 8010*2;
struct edge{
    int to,v,next;
}ed[maxn*4];
int head[maxn],cnte = 0;
void ae(int x,int y){//addedge
    ed[++cnte].to = y;
    ed[cnte].next=head[x];
    head[x]=cnte;
}
int n,m;
int col[maxn];
int pre[maxn];int cntp = 0;
//判断x点能不能选 因为x点能不能选需要选后边的点来试。。
bool dfs(int x){
    if(col[x]==1) return true;
    if(col[x^1]==1) return false;
    col[x]=1,col[x^1]=-1;
    pre[cntp++]=x;
    for(int i=head[x];i!=-1;i=ed[i].next){
        int to = ed[i].to;
        if(!dfs(to)){
            return false;
        }
    }
    return true;
}
bool solve(){
    for(int i = 0;i < 2*n;i+=2){
        cntp = 0;
        if(!dfs(i)){//如果第一个点选不了
            for(int j=0;j<cntp;j++){
                col[pre[j]] = col[pre[j]^1] = 0;
            }
            if(!dfs(i+1)){//如果第二个点也选不了 无解
                return false;
            }
        }
        else{//选第一个点
            col[i]=1,col[i^1]=-1;
        }
    }
    return true;
}
int main(){
    int flag = 0,a,b;
    while(~scanf("%d%d",&n,&m)){
        memset(head,-1,sizeof(head));
        memset(col,0,sizeof(col));
        cntp = 0,flag=0,cnte=0;
        for(int i = 1;i <= m;i++){
            scanf("%d%d",&a,&b);a--,b--;
            ae(a,b^1);ae(b,a^1);
        }
        flag = solve();
        for(int i = 0;i < 2*n && flag == 1;i++){
            if(col[i]==1){
                printf("%d\n",i+1);
            }
        }
        if(flag == 0) printf("NIE\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值