【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");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值