您的位置:首页 > 其它

Hdu 3062 Party

2016-04-01 20:28 232 查看


Party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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 

 

Output

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

否则输出 NO 

 

Sample Input

2
1
0 1 1 1

 

Sample Output

YES

 

Source

2009 Multi-University Training Contest 16 - Host
by NIT

思路:假设有夫妻<A,A’>,<B,B’>,如果A和B之间有矛盾,则建边<A,B’>,<B,A’>
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=2010;
bool Instack[maxn];
int Stack[maxn],Low[maxn],DFN[maxn],Belong[maxn];
int Index,top,scc;
int head[maxn],tot;
struct Edge{
int to,next;
}e[maxn*maxn];

void init(){
tot=0;
memset(head,-1,sizeof(head));
}

void addedge(int from,int to){
e[tot].to=to;
e[tot].next=head[from];
head[from]=tot++;
}

void Tarjan(int u){
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(!DFN[v]){
Tarjan(v);
Low[u]=min(Low[v],Low[u]);
}
else if(Instack[v])
Low[u]=min(DFN[v],Low[u]);
}
int v;
if(Low[u]==DFN[u]){
++scc;
do{
v=Stack[--top];
Instack[v]=false;
Belong[v]=scc;
}while(v!=u);
}
}

bool solveable(int n){
memset(DFN,0,sizeof(DFN));
memset(Low,0,sizeof(Low));
memset(Instack,false,sizeof(Instack));
Index=scc=top=0;
for(int i=0;i<n;i++)
if(!DFN[i])
Tarjan(i);
for(int i=0;i<n;i+=2)
if(Belong[i]==Belong[i^1])
return false;
return true;
}

int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
int u,v,x,y;
for(int i=1;i<=m;i++){
scanf("%d%d%d%d",&u,&v,&x,&y);
addedge(u*2+x,(v*2+y)^1);
addedge(v*2+y,(u*2+x)^1);
}
if(solveable(2*n))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: