hdu 3062 2-sta

来源:互联网 发布:淘宝店铺绑定分店过程 编辑:程序博客网 时间:2024/06/06 00:38
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

对于(a,a'),(b,b'),如果a,b构成仇恨关系,那么如果选a,则必须选b',选b则必须选a',建边a->b',b->a'表示必须关系,也就是说如果选了连通分量里的一个点,那么其余所有边都必须选,则本体a,a'在一个连通分量里为非法,只需要判断就好啦。。。
#include <bits/stdc++.h>using namespace std;struct node{    int u,next;}e[3000*3000];int head[3000],top,cnt,tot,dfn[3000],low[3000],scnt[3000];void add(int u,int v){    e[top].u=v;    e[top].next=head[u];    head[u]=top++;}stack<int>q;void tarjan(int x){    dfn[x]=low[x]=++tot;    q.push(x);    for(int i=head[x];i!=-1;i=e[i].next)    {        int u=e[i].u;        if(!dfn[u])        {            tarjan(u);            low[x]=min(low[x],low[u]);        }        else if(!scnt[u])            low[x]=min(dfn[u],low[x]);    }    if(low[x]==dfn[x])    {        int v=-1;        cnt++;        while(v!=x)        {            v=q.top();            q.pop();            scnt[v]=cnt;        }    }}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        memset(head,-1,sizeof(head));        top=cnt=tot=0;        for(int i=0;i<m;i++)        {            int a1,a2,c1,c2;            scanf("%d%d%d%d",&a1,&a2,&c1,&c2);            int a=a1*2+c1;            int b=a2*2+c2;            add(a,b^1);            add(b,a^1);        }        memset(dfn,0,sizeof(dfn));        memset(low,0,sizeof(low));        memset(scnt,0,sizeof(scnt));        for(int i=0;i<2*n;i++)        {            if(!dfn[i])                tarjan(i);        }        int flag=0;        for(int i=0;i<n;i++)        {            if(scnt[i*2]==scnt[i*2+1])            {                flag=1;                printf("NO\n");                break;            }        }        if(!flag)        {            printf("YES\n");        }    }}

原创粉丝点击