题目地址:POJ 3678
算是2-SAT裸题了。。分类讨论就行了。。
代码如下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define LL __int64
const int INF=0x3f3f3f3f;
int head[2100], cnt, index, top, ans;
int dfn[2100], low[2100], belong[2100], instack[2100], stak[2100];
struct node
{
int u, v, next;
}edge[10000000];
void add(int u, int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void tarjan(int u)
{
dfn[u]=low[u]=++index;
instack[u]=1;
stak[++top]=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
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])
{
ans++;
while(1)
{
int v=stak[top--];
belong[v]=ans;
instack[v]=0;
if(u==v) break;
}
}
}
void init()
{
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
cnt=ans=top=index=0;
}
int main()
{
int n, m, i, j, a, b, c;
char s[10];
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
while(m--)
{
scanf("%d%d%d%s",&a,&b,&c,s);
if(!strcmp(s,"AND"))
{
if(c==0)
{
add(a<<1,b<<1|1);
add(b<<1,a<<1|1);
}
else
{
add(a<<1,b<<1);
add(b<<1,a<<1);
add(a<<1|1,a<<1);
add(b<<1|1,b<<1);
}
}
else if(!strcmp(s,"OR"))
{
if(c==0)
{
add(a<<1|1,b<<1|1);
add(b<<1|1,a<<1|1);
add(a<<1,a<<1|1);
add(b<<1,b<<1|1);
}
else
{
add(a<<1|1,b<<1);
add(b<<1|1,a<<1);
}
}
else
{
if(c==0)
{
add(a<<1|1,b<<1|1);
add(a<<1,b<<1);
add(b<<1|1,a<<1|1);
add(b<<1,a<<1);
}
else
{
add(a<<1|1,b<<1);
add(a<<1,b<<1|1);
add(b<<1|1,a<<1);
add(b<<1,a<<1|1);
}
}
}
for(i=0;i<n<<1;i++)
{
if(!dfn[i])
tarjan(i);
}
int flag=0;
for(i=0;i<n;i++)
{
if(belong[i<<1]==belong[i<<1|1])
{
flag=1;
break;
}
}
if(flag) puts("NO");
else puts("YES");
}
return 0;
}
原文:http://blog.csdn.net/scf0920/article/details/40893923