Description
Input
Output
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; }