【2-SAT】 HDU 3062 Party 裸题

只会水体拉~

一对夫妻看成一个点的YES and NO 

该方法效率略低。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 1040;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 0x3f3f3f3f;
const int mod = 10000007;
struct twosat
{
    int n;
    vector<int>G[MAXN<<1];
    bool mark[MAXN<<1];
    int s[MAXN<<1],c;
    bool dfs(int x)
    {
        if(mark[x^1]) return false;
        if(mark[x]) return true;
        mark[x]=true;
        s[c++]=x;
        for(int i=0;i<G[x].size();i++)
            if(!dfs(G[x][i])) return false;
        return true;
    }
    void init(int n)
    {
        for(int i=0;i<n*2;i++) G[i].clear();
        cler(mark,0);
    }
    void add_clause(int x,int xval,int y,int yval)
    {
        x=x*2+xval;
        y=y*2+yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);
    }
    bool solve()
    {
        for(int i=0;i<2*n;i+=2)
        {
            if(!mark[i]&&!mark[i+1])
            {
                c=0;
                if(!dfs(i))
                {
                    while(c>0) mark[s[--c]]=false;
                    if(!dfs(i+1)) return false;
                }
            }
        }
        return true;
    }
}ac;
int main()
{
    int n;
    //IN;
    while(scanf("%d",&ac.n)!=EOF)
    {

        ac.init(ac.n);
        int m,a,b,c,d;
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            ac.add_clause(a,c,b,d);
        }
        if(ac.solve()) puts("YES");
        else puts("NO");
    }
}


posted @ 2014-09-18 21:26  kewowlo  阅读(125)  评论(0编辑  收藏  举报