HDU - 1814 Peaceful Commission(一个粗暴的2-sat)

HDU - 1814 

题意:直白的2-sat,但是这个是要输出最小的字典序,那么我们只能选择暴力。

思路:我的暴力的想法是,枚举每对元素,先优先取靠前的那个,把它所有连了边的都取了然后打tag,如果它不会产生矛盾,那么继续,如果有,那么先把所有的tag,再判断a+1有没有矛盾,如果a+1也有矛盾,那这组数据就已经gg了。我已经是个蠢到取消tag也要用dfs的人了,看了别人的暴力...基本都是用栈保留然后取消tag,我试了一下,我的两波dfs要跑2s.....而用栈处理.只需要1s不到。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 8e3 + 50, maxe = 2e5;
struct node
{
    int to,next;
    node(int a,int b){to = a; next = b;}
    node(){}
}edge[maxe];
int h[maxn<<1];

int n,m,edgenum,snum=0;
int vis[maxn],col[maxn],ans[maxn],S[maxn];
void add(int f,int t)
{
    edge[edgenum] = node(t,h[f]);
    h[f] = edgenum++;
}
int flag = 0;
bool dfs(int u,int root)
{
    if(col[u^1] != -1) return 0;
    if(col[u] != -1) return 1;
    col[u] = root;
    for(int i = h[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if(dfs(v,root) == 0) return 0;
    }
    return true;
}
void clean(int u)
{
    col[u] = -1;
    for(int i = h[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if(col[v] != -1)
            clean(v);
    }
}
int main()
{
    int a,b;
    while(~scanf("%d%d",&n,&m))
    {

        edgenum = snum = 0;
        for(int i = 0; i < n*2 ; i++) h[i] = -1,col[i] = -1;
        for(int i = 0; i < m ; i++)
        {
            scanf("%d%d",&a,&b); a--,b--;
            add(a,b^1); add(b,a^1);
        }
        int mark = 0;
        int num = 0;
        for(int i = 0; i < n*2 ; i += 2)
        {
            if(col[i] == -1 && col[i+1] == -1)
            {
                if(dfs(i,i)) continue;
                else
                {
                    clean(i);
                    if(dfs(i+1,i+1))    continue;
                    else {mark = 1;break;}
                }
            }
        }
        if(mark) {printf("NIE\n");}
        else
        {
            for(int i = 0; i < n*2; i++)
                if(col[i]!=-1)printf("%d\n",i+1);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值