题目链接 : HDU3062
Party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3039 Accepted Submission(s): 946
Problem 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
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
否则输出 NO
Sample Input
2 1 0 1 1 1
Sample Output
YES
Source
Recommend
lcy
2-SAT 问题
判断是否有解
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
vector<int> gra[2005];
bool vis[2005];
int stack[2005],k;
int low[2005],dfn[2005],scc[2005];
int timer,cnt;
void tarjan_scc(int s)
{
vis[s]=1;
low[s]=dfn[s]=++timer;
stack[k++]=s;
int to;
for(int i=0;i<gra[s].size();i++)
{
to=gra[s][i];
if(dfn[to]==0)
{
tarjan_scc(to);
low[s]=min(low[to],low[s]);
}
else if(vis[to]) low[s]=min(dfn[to],low[s]);
}
if(dfn[s]==low[s])
{
do
{
to=stack[--k];
vis[to]=0;
scc[to]=cnt;
}while(stack[k]!=s);
cnt++;
}
}
int main()
{
// freopen("1.txt","r",stdin);
int n,m;
int a,b,c,d;
while(scanf("%d%d",&n,&m)==2)
{
n<<=1;
for(int i=1;i<=n;i++)
gra[i].clear();
for(int i=0;i<m;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
a=2*a+1;b=2*b+1;
gra[a+c].push_back(b+(!d));
gra[b+d].push_back(a+(!c));
}
memset(scc,0,sizeof(scc));
memset(dfn,0,sizeof(dfn));
cnt=1;timer=0;
bool flag=1;
for(int i=1;i<n;i+=2)
{
if(scc[i]==0 && scc[i+1]==0) tarjan_scc(i);
if(scc[i]==scc[i+1])
{
flag=0;break;
}
}
if(flag)
{
printf("YES\n");
}
else printf("NO\n");
}
return 0;
}