hdu_3062_Party(2-SAT)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3062/
题意:2-SAT的裸题
题解:直接上模版
1 #include<cstdio> 2 #include<cstring> 3 const int MAXN = 2020; 4 const int MAXM = 1000010; 5 struct Edge { int to, next; }edge[MAXM]; 6 int head[MAXN],tot,S[MAXN],top;//栈 ; 7 void init(){tot = 0;memset(head, -1, sizeof(head));} 8 void addedge(int u, int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } 9 bool vis[MAXN];//染色标记,为true表示选择 10 bool dfs(int u) { 11 if(vis[u^1])return false; 12 if(vis[u])return true; 13 vis[u] = true, S[top++] = u; 14 for(int i = head[u];i != -1;i = edge[i].next) 15 if(!dfs(edge[i].to))return false; 16 return true; 17 } 18 bool Twosat(int n){ 19 memset(vis,false,sizeof(vis)); 20 for(int i = 0;i < n;i += 2){ 21 if(vis[i] || vis[i^1])continue; 22 top = 0; 23 if(!dfs(i)){ 24 while(top)vis[S[--top]] = false; 25 if(!dfs(i^1)) return false; 26 } 27 } 28 return true; 29 } 30 int main(){ 31 int n,m,u,v,mm,ww; 32 while(~scanf("%d%d",&n,&m)){ 33 init(); 34 while(m--){ 35 scanf("%d%d%d%d",&u,&v,&mm,&ww); 36 u=u*2+mm,v=v*2+ww; 37 addedge(u,v^1); 38 addedge(v,u^1); 39 } 40 if(Twosat(2*n))puts("YES"); 41 else puts("NO"); 42 } 43 return 0; 44 }