hdu_3062_Party(2-SAT)

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

题意:2-SAT的裸题

题解:直接上模版

#include
#include
const int MAXN = 2020; 
const int MAXM = 1000010;
struct Edge { int to, next; }edge[MAXM]; 
int head[MAXN],tot,S[MAXN],top;//栈 ;
void init(){tot = 0;memset(head, -1, 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^1])return false; 
	if(vis[u])return true; 
	vis[u] = true, S[top++] = u;
	for(int i = head[u];i != -1;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 = 0;i < n;i += 2){
		if(vis[i] || vis[i^1])continue;  
		top = 0; 
		if(!dfs(i)){  
			while(top)vis[S[--top]] = false;    
			if(!dfs(i^1)) 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*2+mm,v=v*2+ww;
			addedge(u,v^1);      
			addedge(v,u^1);     
		}      
		if(Twosat(2*n))puts("YES");
		else puts("NO"); 
	}    
	return 0; 
} 


你可能感兴趣的:(hdu,模版)