hdu_3062_Party(2-SAT)

时间:2021-08-14 04:55:40

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3062/

题意:2-SAT的裸题

题解:直接上模版

 #include<cstdio>
#include<cstring>
const int MAXN = ;
const int MAXM = ;
struct Edge { int to, next; }edge[MAXM];
int head[MAXN],tot,S[MAXN],top;//栈 ;
void init(){tot = ;memset(head, -, sizeof(head));}
void addedge(int u, int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; }
bool vis[MAXN];//染色标记,为true表示选择
bool dfs(int u) {
if(vis[u^])return false;
if(vis[u])return true;
vis[u] = true, S[top++] = u;
for(int i = head[u];i != -;i = edge[i].next)
if(!dfs(edge[i].to))return false;
return true;
}
bool Twosat(int n){
memset(vis,false,sizeof(vis));
for(int i = ;i < n;i += ){
if(vis[i] || vis[i^])continue;
top = ;
if(!dfs(i)){
while(top)vis[S[--top]] = false;
if(!dfs(i^)) return false;
}
}
return true;
}
int main(){
int n,m,u,v,mm,ww;
while(~scanf("%d%d",&n,&m)){
init();
while(m--){
scanf("%d%d%d%d",&u,&v,&mm,&ww);
u=u*+mm,v=v*+ww;
addedge(u,v^);
addedge(v,u^);
}
if(Twosat(*n))puts("YES");
else puts("NO");
}
return ;
}