Party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6669 Accepted Submission(s): 2187
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
2-sat裸题
被多组数据坑了。。
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)x=-x,putchar('-');if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=2010;
int n,m;
int ecnt,last[N];
struct EDGE{int to,nt;}e[N*N];
inline void add(int u,int v)
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;}
int a[N][2],dfn[N],low[N],s[N],top,scc,cnt,bel[N];
bool ins[N];
void tarjan(int u)
{
dfn[u]=low[u]=++cnt;
ins[u]=1;s[++top]=u;
for(int i=last[u];i;i=e[i].nt)
{
if(!dfn[e[i].to])tarjan(e[i].to),low[u]=min(low[u],low[e[i].to]);
else if(ins[e[i].to]&&dfn[e[i].to]<low[u])low[u]=dfn[e[i].to];
}
if(dfn[u]==low[u])
{
scc++;int tmp;
do
{
tmp=s[top--];ins[tmp]=0;bel[tmp]=scc;
}while(tmp^u);
}
}
int main()
{
register int i,a1,a2,c1,c2,flag;
while(scanf("%d%d",&n,&m)!=EOF)
{
a1=flag=ecnt=top=scc=cnt=0;
memset(last,0,sizeof(last));
memset(bel,0,sizeof(bel));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(s,0,sizeof(s));
for(i=1;i<=n;++i)a[i][0]=++a1,a[i][1]=++a1;
for(i=1;i<=m;++i)
{
a1=read()+1;a2=read()+1;c1=read();c2=read();
add(a[a2][c2],a[a1][c1^1]);add(a[a1][c1],a[a2][c2^1]);
}
for(i=1;i<=n*2;++i)if(!dfn[i])tarjan(i);
for(i=1;i<=n;++i)if(bel[a[i][0]]==bel[a[i][1]]){puts("NO");flag=1;break;}
if(!flag)puts("YES");
}
return 0;
}