题目链接:
Katu Puzzle
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12317 Accepted: 4540 Description
Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integer c (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertex Vi a value Xi (0 ≤ Xi ≤ 1) such that for each edge e(a, b) labeled by op and c, the following formula holds:
Xa op Xb = c
The calculating rules are:
AND 0 1 0 0 0 1 0 1
OR 0 1 0 0 1 1 1 1
XOR 0 1 0 0 1 1 1 0 Given a Katu Puzzle, your task is to determine whether it is solvable.
Input
The first line contains two integers N (1 ≤ N ≤ 1000) and M,(0 ≤ M ≤ 1,000,000) indicating the number of vertices and edges.
The following M lines contain three integers a (0 ≤ a < N), b(0 ≤ b < N), c and an operator op each, describing the edges.Output
Output a line containing "YES" or "NO".
Sample Input
4 4 0 1 1 AND 1 2 1 OR 3 2 0 AND 3 0 0 XOR
Sample Output
YES
Hint
X0 = 1, X1 = 1, X2 = 0, X3 = 1.
Source
题目大意:
一个N个顶点和M条边的有向图,每个顶点能取0或1两个值.现在每条边被一个操作符(or,and,xor)以及一个值(0或1)标记了,表示a与b按操作符运算的结果是值(0或1).问你该有向图是否有可行解?
分析 :
A AND B = C
- 当c==1 ,表示 A AND B = 1,即A=1且B=1,转换成保证A=1的蕴含式是(非A->A),因为只有0->1是真,B=1同理,既有 <a,0,a,1>,<b,0,b,1>;
- 当c==0 ,表示 A AND B = 0,即A=0或者B=0,所以<a,0,b,1> <a,1,b,0> ,
A OR B = C
- 当c==0,类似同理上面1
- 当c==1,类似同理上面2
A XOR B = c
- 当c==1,两种情况,A !=B
- 当c==0,两种情况,A ==B
将与,或,异或门写成等价的蕴涵门形式:
A AND B = 1 | !A->A | !B->B | ||
A AND B = 0 | A->!B | B->!A | ||
A OR B = 1 | !A->B | !B->A | ||
A OR B = 0 | A->!A | B->!B | ||
A XOR B = 1 | A->!B | !B->A | !A->B | B->!A |
A XOR B = 0 | A->B | B->A | !A->!B | !B->!A |
表格中的蕴涵表达式之间的运算符为与,证明很简单,利用A->B = !A OR B进行布尔运算,或者干脆列个真值表也行。
This is the code:
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
int read()//输入外挂
{
int ret=0, flag=0;
char ch;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
ret = ch - '0';
while((ch=getchar())>='0'&&ch<='9')
ret=ret*10+(ch-'0');
return flag ? -ret : ret;
}
const int maxn=1000000+10;
struct TwoSAT
{
int n;//原始图的节点数(未翻倍)
vector<int> G[maxn*2];//G[i]==j表示如果mark[i]=true,那么mark[j]也要=true
bool mark[maxn*2];//标记
int S[maxn*2],c;//S和c用来记录一次dfs遍历的所有节点编号
void init(int n)
{
this->n=n;
for(int i=0; i<2*n; i++)
G[i].clear();
memset(mark,0,sizeof(mark));
}
//加入(x,xval)或(y,yval)条件
//xval=0表示假,yval=1表示真
void add_or_clause(int x,int xval,int y,int yval)//表示或的意思
{
x=x*2+xval;
y=y*2+yval;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
//加入(x,xval)且(y,yval)条件
//xval=0表示假,yval=1表示真
void add_and_clause(int x,int xval,int y,int yval)//表示或的意思
{
x=x*2+xval;
y=y*2+yval;
G[x].push_back(y);
}
//从x执行dfs遍历,途径的所有点都标记
//如果不能标记,那么返回false
bool dfs(int x)
{
if(mark[x^1])
return false;//这两句的位置不能调换
if(mark[x])
return true;
mark[x]=true;
S[c++]=x;
for(int i=0; i<G[x].size(); i++)
if(!dfs(G[x][i]))
return false;
return true;
}
//判断当前2-SAT问题是否有解
bool solve()
{
for(int i=0; i<2*n; i+=2)
if(!mark[i] && !mark[i+1])
{
c=0;
if(!dfs(i))
{
while(c>0)
mark[S[--c]]=false;
if(!dfs(i+1))
return false;
}
}
return true;
}
} TS;
int main()
{
//freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
//freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
int n,m;
while(~scanf("%d%d",&n,&m))
{
TS.init(n);
while(m--)
{
int a,b,c;
char s[10];
scanf("%d%d%d%s",&a,&b,&c,s);
if(s[0]=='A')
{
if(c==1)
{
TS.add_and_clause(a,0,a,1);
TS.add_and_clause(b,0,b,1);
}
else
{
TS.add_and_clause(a,1,b,0);
TS.add_and_clause(b,1,a,0);
}
}
else if(s[0]=='O')
{
if(c==0)
{
TS.add_and_clause(a,1,a,0);
TS.add_and_clause(b,1,b,0);
}
else
{
TS.add_and_clause(a,0,b,1);
TS.add_and_clause(b,0,a,1);
}
}
else
{
if(c==1)
{
TS.add_and_clause(a,0,b,1);
TS.add_and_clause(b,0,a,1);
TS.add_and_clause(a,1,b,0);
TS.add_and_clause(b,1,a,0);
}
else
{
TS.add_and_clause(a,1,b,1);
TS.add_and_clause(b,1,a,1);
TS.add_and_clause(a,0,b,0);
TS.add_and_clause(b,0,a,0);
}
}
}
bool flag=TS.solve();
if(flag)
puts("YES");
else
puts("NO");
}
return 0;
}