POJ-3678 Katu Puzzle 2sat

  题目链接:http://poj.org/problem?id=3678

  分别对and,or,xor推出相对应的逻辑关系:

    逻辑关系      1              0

     A and B     A'->A,B'->B          A->B',B->A'

     A or B   A'->B',B'->A          A->A',B->B'

     A xor B     A'->B,B'->A,A->B',B->A'      A->B,A'->B'

 //STATUS:C++_AC_96MS_472KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int first[N*],next[N*N*],vis[N*],S[N*];
int n,m,mt,cnt; struct Edge{
int u,v;
}e[N*N*]; void adde(int a,int b)
{
e[mt].u=a,e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
} int dfs(int u)
{
if(vis[u^])return ;
if(vis[u])return ;
int i;
vis[u]=;
S[cnt++]=u;
for(i=first[u];i!=-;i=next[i]){
if(!dfs(e[i].v))return ;
}
return ;
} int Twosat()
{
int i,j;
mem(vis,);
for(i=;i<n;i+=){
if(vis[i] || vis[i^])continue;
cnt=;
if(!dfs(i)){
while(cnt)vis[S[--cnt]]=;
if(!dfs(i^))return ;
}
}
return ;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,b,c;
char op[];
while(~scanf("%d%d",&n,&m) && (n||m))
{
n<<=;
mem(first,-);mt=;
while(m--){
scanf("%d%d%d%s",&a,&b,&c,op);
a<<=,b<<=;
if(op[]=='A'){
if(c){
adde(a^,a);
adde(b^,a);
}
else {
adde(a,b^);
adde(b,a^);
}
}
else if(op[]=='O'){
if(c){
adde(a^,b);
adde(b^,a);
}
else {
adde(a,a^);
adde(b,b^);
}
}
else {
if(c){
adde(a^,b);
adde(b^,a);
adde(a,b^);
adde(b,a^);
}
else {
adde(a,b);
adde(a^,b^);
}
}
} printf("%s\n",Twosat()?"YES":"NO");
}
return ;
}
上一篇:Freemarker详细解释


下一篇:Linux学习之CentOS(二)--初识linux的一些常用命令(基础命令)