[Question link]
Http://poj.org/problem? Id = 3905
[Idea]
2-simple sat 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", & N, & M) {G. init (); For (INT I = 0; I <m; ++ I) {scanf ("% 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 =-B; -- A; -- B; G. addedge (a + N, B + n); G. addedge (B, A);} else if (a <0 & B> 0) {//-+ A =-A; -- B; G. addedge (a, B); G. addedge (B + N, A + n);} else if (a <0 & B <0) {//--A =-A; B =-B; -- A; -- B; G. addedge (A, B + n); G. addedge (B, A + n) ;}} if (SAT. check (G, n) puts ("1"); else puts ("0");} return 0 ;}