Party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3120 Accepted Submission(s): 974
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
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int VM=2010; const int EM=4000010; struct Edge{ int to,nxt; }edge[EM<<1]; int n,m,cnt,dep,top,atype,head[VM]; int dfn[VM],low[VM],vis[VM],belong[VM]; int stack[VM]; void Init(){ cnt=0, atype=0, dep=0, top=0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(belong,0,sizeof(belong)); } void addedge(int cu,int cv){ edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++; } void Tarjan(int u){ dfn[u]=low[u]=++dep; stack[top++]=u; vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; if(!dfn[v]){ Tarjan(v); low[u]=min(low[u],low[v]); }else if(vis[v]) low[u]=min(low[u],dfn[v]); } int j; if(dfn[u]==low[u]){ atype++; do{ j=stack[--top]; belong[j]=atype; vis[j]=0; }while(u!=j); } } int solve(){ for(int i=0;i<2*n;i++) if(!dfn[i]) Tarjan(i); for(int i=0;i<n;i++) if(belong[i]==belong[i+n]) //如果夫妻在同一个强连通分量中,那么就不存在解 return 0; return 1; } int select(int x,int n){ return x>n?x-n:x+n; } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){ Init(); int a1,a2,c1,c2; while(m--){ scanf("%d%d%d%d",&a1,&a2,&c1,&c2); a1=a1+c1*n; a2=a2+c2*n; addedge(a1,select(a2,n)); addedge(a2,select(a1,n)); } if(solve()) puts("YES"); else puts("NO"); } return 0; }