HDU 3062 Party(2-SAT简单题)

题意:中文题

思路:2-SAT入门



#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 10000+10;
struct TwoSAT
{
	int n;
	vector<int>G[maxn*2];
	bool mark[maxn*2];
	int S[maxn*2],c;

	void init(int n)
	{
		this->n = n;
		for (int i = 0;i<2*n;i++)
			G[i].clear();
		memset(mark,0,sizeof(mark));
	}

	void add_clause(int x,int xval,int y,int yval)
	{
		x = x*2+xval;
		y = y*2+yval;
		G[x].push_back(y);
//		G[y^1].push_back(x);
	}
	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;
	}
	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;
	}
}TS;

int main()
{
    int n,m;
	while (scanf("%d%d",&n,&m)!=EOF)
	{
        TS.init(n);
	    int a,b,va,vb;
	    while (m--)
		{
			scanf("%d%d%d%d",&a,&b,&va,&vb);
			TS.add_clause(a,va,b,vb^1);
			TS.add_clause(b,vb,a,va^1);
		}	
		printf("%s\n",TS.solve()?"YES":"NO");
	}
}

Description

有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n 个人同时列席?
 

Input

n: 表示有n对夫妻被邀请 (n<= 1000) 
m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1)) 

在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2 
A1,A2分别表示是夫妻的编号 
C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫 
夫妻编号从 0 到 n -1 
 

Output

如果存在一种情况 则输出YES 
否则输出 NO 
 

Sample Input

       
       
2 1 0 1 1 1
 

Sample Output

       
       
YES
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值