Ideas
2-sat a simple judgment question
Code
#include <iostream> #include <queue> #include <cstdio> #include <cstring> using namespace std
;
const int MAXN = 1010;
const int VN = MAXN*2;
const int EN = VN*VN*2;
int n, m;
struct edge{int V, next;
};
struct graph{int size, HEAD[VN];
Edge E[en];
void Init () {size=0; memset (Head,-1, sizeof (head));
void Addedge (int u, int v) {e[size].v = v;
E[size].next = Head[u];
Head[u] = size++;
}}g;
Class two_sat{Public:bool Check (const graph& g, const int n) {SCC (g, 2*n);
for (int i=0; i<n; ++i) if (belong[i] = = Belong[i+n]) return false;
return true;
} private:void Tarjan (const graph& g, const int u) {int V;
Low[u] = dfn[u] = ++idx;
sta[top++] = u;
Instack[u] = true; for (int e=g.head[u]; E!=-1 E=g.e[e].next) {v = g.e[e].v;
if (dfn[v] = =-1) {Tarjan (g, v);
Low[u] = min (Low[u], low[v]);
}else if (Instack[v]) {Low[u] = min (Low[u], dfn[v]);
} if (dfn[u] = = Low[u]) {++bcnt;
do{v = sta[--top];
INSTACK[V] = false;
BELONG[V] = bcnt;
}while (U!= v);
} void SCC (const graph& g, const int n) {bcnt = idx = top = 0;
memset (DFN,-1, sizeof (DFN));
memset (instack, 0, sizeof (instack));
for (int i=0; i<n; ++i) if (dfn[i] = = 1) Tarjan (g, I);
} private:int idx, top, bcnt;
int DFN[VN], low[vn], BELONG[VN], STA[VN];
BOOL INSTACK[VN];
}sat;
int main () {int A, B;
while (~SCANF ("%d%d", &n, &m)) { G.init ();
for (int i=0; i<m; ++i) {scanf ("%d%d", &a, &b);
if (a>0 && b>0) {//+ +--a;--b;
G.addedge (a+n, b);
G.addedge (B+n, a);
}else if (a>0 && b<0) {//+-B =-C; --a;
--b;
G.addedge (A+n, b+n);
G.addedge (b, a);
}else if (a<0 && b>0) {//-+ a =-A; --a;
--b;
G.addedge (A, b);
G.addedge (B+n, a+n);
}else if (a<0 && b<0) {//--A =-A; b =-A; --a;
--b;
G.addedge (A, b+n);
G.addedge (b, a+n);
} if (Sat.check (g, N)) puts ("1");
Else puts ("0");
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/