hdu 3062 Party 2-SAT入门

Party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2822    Accepted Submission(s): 878


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

---------------------------

由于一对夫妻只出现一人,可以只考虑丈夫一人的情况,建立丈夫参加,丈夫不参加两个点。若无矛盾,则存在n人列席的情况。


---------------------------

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=4444;
const int maxm=1111111;

struct EDGE{
    int to;
    int w;
    int next;
}edges[maxm];

int head[maxn];
int edge,node;

int lowlink[maxn],pre[maxn],sccno[maxn],stk[maxn],top,dfs_clock,scc_cnt;

void prepare(int _node)
{
    node=_node;
    for (int i=0;i<=node;i++) head[i]=-1;
    edge=0;
}

void addedge(int u,int v,int c=1)
{
    edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
}


void Tarjan_dfs(int u)
{
    int i,v;
    pre[u]=lowlink[u]=++dfs_clock;
    stk[top++]=u;
    for(i=head[u];i!=-1;i=edges[i].next)
        if(!pre[v=edges[i].to])
            Tarjan_dfs(v),lowlink[u]=min(lowlink[u],lowlink[v]);
        else if(!sccno[v])
            lowlink[u]=min(lowlink[u],pre[v]);
    if(pre[u]==lowlink[u])
    {
        sccno[u]=++scc_cnt;
        while(stk[--top]!=u)sccno[stk[top]]=scc_cnt;
    }
}

void Tarjan_find_scc()
{
    int i;
    scc_cnt=dfs_clock=top=0;
    memset(sccno,0,sizeof(sccno));
    memset(pre,0,sizeof(pre));
    for(i=0;i<node;i++)
        if(!pre[i]) Tarjan_dfs(i);
}

bool two_sat()
{
    for(int i=0;i<node;i+=2)
        if(sccno[i]==sccno[i^1])return false;
    return true;
}

int main()
{
    int n,m;
    int a1,a2,c1,c2;
    while (~scanf("%d",&n))
    {
        scanf("%d",&m);
        prepare(n+n);
        while (m--)
        {
            scanf("%d%d%d%d",&a1,&a2,&c1,&c2);
            addedge( (a1<<1)+c1, (a2<<1|1)-c2 );
            addedge( (a2<<1)+c2, (a1<<1|1)-c1 );
        }
        Tarjan_find_scc();
        if (two_sat())
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}





posted on 2013-04-17 21:12  电子幼体  阅读(133)  评论(0编辑  收藏  举报

导航