[题目链接]
http://poj.org/problem?id=3678
[算法]
2-SAT
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 2010 #define MAXM 1000010 int i,n,m,tot,timer,cnt,a,b,c; int head[MAXN],belong[MAXN],dfn[MAXN],low[MAXN]; bool instack[MAXN]; string opt; bool flag; stack< int > s; struct edge { int to,nxt; } e[MAXM]; inline void addedge(int u,int v) { tot++; e[tot] = (edge){v,head[u]}; head[u] = tot; } inline void tarjan(int u) { int i,v; low[u] = dfn[u] = ++timer; instack[u] = true; s.push(u); for (i = head[u]; i; i = e[i].nxt) { v = e[i].to; if (!dfn[v]) { tarjan(v); low[u] = min(low[u],low[v]); } else if (instack[v]) low[u] = min(low[u],dfn[v]); } if (dfn[u] == low[u]) { cnt++; do { v = s.top(); s.pop(); belong[v] = cnt; instack[v] = false; } while (v != u); } } int main() { ios :: sync_with_stdio(false); cin.tie(0); while (cin >> n >> m) { tot = cnt = timer = 0; while (!s.empty()) s.pop(); memset(head,0,sizeof(head)); memset(belong,0,sizeof(belong)); memset(instack,false,sizeof(instack)); memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); while (m--) { cin >> a >> b >> c >> opt; if (opt == "AND") { if (c == 0) { addedge(a + n,b); addedge(b + n,a); } else { addedge(a,a + n); addedge(b,b + n); } } if (opt == "OR") { if (c == 0) { addedge(a + n,a); addedge(b + n,b); } else { addedge(a,b + n); addedge(b,a + n); } } if (opt == "XOR") { if (c == 0) { addedge(a,b); addedge(b,a); addedge(a + n,b + n); addedge(b + n,a + n); } else { addedge(a,b + n); addedge(b,a + n); addedge(a + n,b); addedge(b + n,a); } } } for (i = 0; i < 2 * n; i++) { if (!dfn[i]) tarjan(i); } flag = true; for (i = 0; i < n; i++) { if (belong[i] == belong[i + n]) { cout<< "NO" << endl; flag = false; break; } } if (flag) cout<< "YES" << endl; } return 0; }