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
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
裸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;
}