A - Party


Crawling in process...

Crawling failed

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u


​Submit​​​ ​​​Status​


System Crawler (2013-05-30)



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

模板不解释。

#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=2010;
class Edge
{
public:int v,next;
};
class TWO_SAT
{
public:
int dfn[mm],e_to[mm],stack[mm];
Edge e[mm*mm*2];
int edge,head[mm],top,dfs_clock,bcc;
void clear()
{
edge=0;clr(head,-1);
}
void add(int u,int v)
{
e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
}
void add_my(int x,int xval,int y,int yval)
{
x=x+x+xval;y=y+y+yval;
add(x,y);
}
void add_clause(int x,int xval,int y,int yval)
{///x or y
x=x+x+xval;
y=y+y+yval;
add(x^1,y);add(y^1,x);
}
int tarjan(int u)
{
int lowu,lowv;
lowu=dfn[u]=++dfs_clock;
int v; stack[top++]=u;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(!dfn[v])
{
lowv=tarjan(v);
lowu=min(lowv,lowu);
}
else if(e_to[v]==-1)//in stack
lowu=min(lowu,dfn[v]);
}
if(dfn[u]==lowu)
{
++bcc;
do{
v=stack[--top];
e_to[v]=bcc;
}while(v!=u);
}
return lowu;
}
bool find_bcc(int n)
{ clr(e_to,-1);
clr(dfn,0);
bcc=dfs_clock=top=0;
FOR(i,0,2*n-1)
if(!dfn[i])
tarjan(i);
for(int i=0;i<2*n;i+=2)
if(e_to[i]==e_to[i^1])return 0;
return 1;
}
}two;
int n,m;
int main()
{ int a,b,c,d;
while(~scanf("%d%d",&n,&m))
{
two.clear();
FOR(i,1,m)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
two.add_clause(a,c,b,d);
}
if(two.find_bcc(n))printf("YES\n");
else printf("NO\n");
}
}