Excerpted from http://www.cnblogs.com/kuangbin/archive/2012/10/05/2712429.html
An existing sequence A of n Boolean values that gives some restrictive relationships, such as a[x] && a[y] = 0, a[x] | | A[y] | | A[z]=1, and so on, to determine the value of a[0..n-1], so that it satisfies all the limiting relationships. This is called the SAT problem, and, in particular, if the limit is limited to only two elements in each restriction relationship, it is called a 2-sat problem.
There are 11 possible limiting relationships due to the limitation of up to two elements in the 2-sat problem:
A[X]
Not a[x]
A[X] and A[y]
A[X] and not a[y]
A[X] OR A[y]
A[X] OR not a[y]
Not (A[x] and a[y])
Not (A[x] OR a[y])
A[X] XOR A[y]
Not (A[x] XOR a[y])
A[X] XOR not a[y]
Further, A[x] and A[y] equivalent (a[x]) and (A[y]) (i.e. can be split into a[x] and A[y] two limiting relationships), not (A[x] OR a[y]) equivalent to not a[x] and not a[y] (that is, can be split into not a[x] With not a[y] two limit relationships). As a result, there are up to 9 possible limiting relationships.
The 2-sat problem is manifested in the following form most of the time: there are n pairs of items, each pair of items must be selected one, and they can only select one, and there are some restrictions between them (such as a two items can not be selected, a two items can not be selected, a two items must and can only choose one, an item is required), Each pair of items can be treated as a Boolean value (pick the first item is equivalent to 0, choose the second equivalent of 1), and if all the limiting relationships are limited to only two items, they can all be converted into 9 basic restriction relationships, which translates into 2-sat models.
"Modeling"
You can construct a g,g graph that contains 2*n vertices, the first n vertices (1~n) to indicate that the I element can be selected, and the last n vertices (n+1~2*n) to indicate that the first element cannot be selected. Ai and A (i+n) cannot be selected at the same time. Similarly ai + Bj = ~ (A (i+n) + B (j+n)), A (i+n) and B (J+n) cannot be selected at the same time. Select ~a, you must select B; if you select ~b, you must select A.
If I to J has a path, then if I select, then J will also be selected, or, if J is not selected, I also can not be selected.
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <queue>using namespace STD;Const intMAXN =2200;Const intMAXM = MAXN*MAXN;structedgenode{intto;intNext;} EDGES[MAXM];intHEAD[MAXN];intDFN[MAXN],LOW[MAXN],BELONG[MAXN],STACK[MAXN],VIS[MAXN];intM,id,lay,scc,n,m;//belong[] To determine if I and i+n are in a strong connected componentvoidAddedges (intUintV) {edges[id].to = v; Edges[id].next = Head[u]; Head[u] = id++;}voidTARBFS (intPOS) {Dfn[pos] = Low[pos] = ++lay; stack[m++] = pos; Vis[pos] =1; for(inti = Head[pos]; I! =-1; i = edges[i].next) {intv = edges[i].to;if(!dfn[v]) {Tarbfs (v); Low[pos] = min (low[pos],low[v]); }Else if(Vis[v]) Low[pos] = min (low[pos],low[v]); }intVif(Dfn[pos] = = Low[pos]) {++SCC; Do{v = stack[--m]; BELONG[V] = SCC; VIS[V] =0; } while(v! = pos); }}intMain () {intU,v; while(~scanf("%d%d", &n,&m)) {id = M = SCC = Lay =0;memset(head,-1,sizeof(Head));memset(Vis,0,sizeof(VIS));memset(Low,0,sizeof(low));memset(DFN,0,sizeof(DFN));memset(Belong,0,sizeof(belong)); for(inti =0; i < M; ++i) {scanf("%d%d", &u,&v);intA =ABS(u);intb =ABS(v);if(U >0&& v >0) {//a, b at least one is selectedAddedges (A+N,B);//If A is not selected, B must be selectedAddedges (B+n,a);//If B is not selected, a must be selected}if(U <0&& v <0) {//a, b at least one is not selectedAddedges (A,b+n);//If A is selected, B must not be selectedAddedges (B,a+n);//If B is selected, a must not be selected}if(U >0&& v <0) {//a is selected and B is not selected two things happen at least oneAddedges (A+n,b+n);//If A is not selected, B must not be selectedAddedges (B,a);//If B is selected, then a must be selected}if(U <0&& v >0) {//a is not selected and B is selected at least one occurrenceAddedges (A, b);//If A is selected, B must be selectedAddedges (B+n,a+n);//If B is not selected, a must not be selected} } for(inti =1; I <=2*n; ++i)if(!dfn[i]) TARBFS (i);intAns =1; for(inti =1; I <= N; ++i) {if(Belong[i] = = Belong[i+n])//If I and i+n are in a connected component, then 2-sat is not satisfied{ans =0; Break; } }printf("%d\n", ans);//Does not exist to meet 2-sat}return 0;}
2-sat "Templates"