HDU 3062 Party 2-SAT

A - Party
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
Appoint description:

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
 

裸2-sat 入门题

ACcode:

#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 100000
using namespace std;
struct Node{
    int to,next;
}edge[maxn];
int head[maxn],tot;
void init(){tot=0;memset(head,-1,sizeof(head));}
void add(int u,int v){edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;}
int low[maxn],dfn[maxn],Stack[maxn],Belong[maxn];
int Index,top;
int scc;
bool Instack[maxn];
int num[maxn];
void tarjan(int u){
    int v;
    low[u]=dfn[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;
    for(int i=head[u];i!=-1;i=edge[i].next){
        v=edge[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(Instack[v]&&low[u]>dfn[v])
            low[u]=dfn[v];
    }
    if(low[u]==dfn[u]){
        scc++;
        do{
            v=Stack[--top];
            Instack[v]=false;
            Belong[v]=scc;
            num[scc]++;
        }
        while(v!=u);
    }
}
bool solvable(int n){
    memset(dfn,0,sizeof(dfn));
    memset(Instack,false,sizeof(Instack));
    memset(num,0,sizeof(num));
    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,a1,a2,c1,c2;
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        while(m--){
            scanf("%d%d%d%d",&a1,&a2,&c1,&c2);
            add(a2*2+c2,(a1*2+c1)^1);
            add(a1*2+c1,(a2*2+c2)^1);
        }
        if(solvable(n*2))printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值