hdu 3062 Party【2-Sat】【经典入门】

http://acm.hdu.edu.cn/showproblem.php?pid=3062

Party

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

 

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

题意:题目所述

分析:

2sat模板题,妻子和丈夫就想象成一个人的两种状态,那么就是两个人的两种状态矛盾,当矛盾就调用add_clause,使不矛盾的建边。

有两个模板,其实都差不多,如果理解强连通,可以看第二个比较好

代码1:

#include<bits/stdc++.h>

using namespace std;
//const int maxn=1000000+10;
struct TwoSAT
{
    int n;//原始图的节点数(未翻倍)
    vector<int> G[2010*2];//G[i]==j表示如果mark[i]=true,那么mark[j]也要=true
    bool mark[2020*2];//标记
    int S[10020*2],c;//S和c用来记录一次dfs遍历的所有节点编号

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<2*n;i++) G[i].clear();
        memset(mark,0,sizeof(mark));
    }

    //加入(x,xval)(y,yval)中有一个是对的,另一个不对
    //xval=0表示假,yval=1表示真
    void add_clause(int x,int xval,int y,int yval)
    {
        x=x*2+xval;
        y=y*2+yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);

    }

    //从x执行dfs遍历,途径的所有点都标记
    //如果不能标记,那么返回false
    bool dfs(int x)
    {
        if(mark[x^1]) return false;//这两句的位置不能调换
        if(mark[x]) return true;
        mark[x]=true;
        S[c++]=x;
        for(int i=0;i<G[x].size();i++)
        {
            if(x==G[x][i])continue;
            if(!dfs(G[x][i])) return false;
        }
        return true;
    }

    //判断当前2-SAT问题是否有解
    bool solve()
    {
        for(int i=0;i<2*n;i+=2)
        if(!mark[i] && !mark[i+1])
        {
            c=0;
            if(!dfs(i))
            {
                while(c>0) mark[S[--c]]=false;
                if(!dfs(i+1)) return false;
            }
        }
        return true;
    }
}fun;

int main()
{
    int i,j,x,y,aa,bb,m,n,cc,dd;
    while(~scanf("%d%d",&n,&m))
    {
        fun.init(n);
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&aa,&bb,&cc,&dd);
            fun.add_clause(aa,cc,bb,dd);
        }
        bool flag;
        flag=fun.solve();


        if(flag)
            printf("YES\n");
        else
            printf("NO\n");

    }

}
/*
2 4
0 1 0 0
0 1 1 1
0 1 1 0
0 1 0 1
*/

代码2:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int head[100000];
struct EdgeNode
{
    int to;
    int w;
    int next;
}e[1000000];
int vis[10000];
int low[10000];
int dfn[10000];
int color[10000];
int stack[10000];
int n,m,cont,sig,cnt,tt;
void add(int from,int to)
{
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void init()
{
    cont=0;
    tt=-1;
    sig=0;
    cnt=1;
    memset(stack,0,sizeof(stack));
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
    memset(color,0,sizeof(color));
    memset(head,-1,sizeof(head));
}
void Tarjan(int u)
{
    vis[u]=1;
    low[u]=dfn[u]=cnt++;
    stack[++tt]=u;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(vis[v]==0)Tarjan(v);
        if(vis[v]==1)low[u]=min(low[u],low[v]);
    }
    if(dfn[u]==low[u])
    {
        sig++;
        do
        {
            vis[stack[tt]]=-1;
            color[stack[tt]]=sig;
        }
        while(stack[tt--]!=u);
    }
}
int Slove()
{
    for(int i=0;i<n*2;i++)
    {
        if(vis[i]==0)
        {
            Tarjan(i);
        }
    }
    for(int i=0;i<n*2;i+=2)
    {
        if(color[i]==color[i+1])return 1;
    }
    return 0;
    
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            int a,b,x,y;
            scanf("%d%d%d%d",&a,&b,&x,&y);
            add(2*a+x,2*b+1-y);
            add(2*b+y,2*a+1-x);
        }
        int tt=Slove();
        if(tt==1)printf("NO\n");
        else printf("YES\n");
    }
}

猜你喜欢

转载自blog.csdn.net/lml11111/article/details/82945347
今日推荐