Problem 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

题意:给出一 n 个点 m 条边的有向图,每个点只能取 0、1,现在每条边被一个操作符 or、and、xor 以及一个值标记了,表示 a、b 按操作符运算的结果,问这个有向图是否有可行解

思路:根据题意,该题是一个 2-SAT 问题,将操作符进行转换,进行判定即可

对于 A[x],可以通过连边 <x',x> 实现,NOT A[x],可以通过连边 <x,x'> 来实现,对于 NOT(A[x] AND A[y]) 需要连两条边 <x, y'> 和 <y, x'> 来实现,对于 A[x] OR A[y] 需要连两条边 <x', y> 和 <y', x> 来实现

故对于 and、or、xor 三种运算有:

and 运算:

  • a and b = 0 时:若 a=1,则必定满足 b=0;若 b=1,则必定满足 a=0,即:<a,1,b,0>、<b,1,a,0>
  • a and b = 1 时:a=1 且 b=1,即:<a,0,a,1>、<b,0,b,1>

or 运算:

  • a or b = 0 时:a = 0 且 b = 0,即:<a,1,a,0>、<b,1,b,0>
  • a or b = 1 时:若 a=0,则必定满足 b=1;若 b=0,则必定满足 a=0,即:<a,0,b,1>、<b,0,a,1>

xor 运算:

  • a xor b = 0 时,有以下四种情况:
    若 a = 0,则必定满足 b = 0,即:<a,0,b,0>
    若 b = 0,则必定满足 a = 0,即:<b,0,a,0>
    若 a = 1,则必定满足 b = 1,即:<a,1,b,1>
    若 b = 1,则必定满足 a = 1,即:<b,1,a,1>
  • a xor b = 1 时,有以下四种情况:
    若 a = 0,则必定满足 b = 1,即:<a,0,b,1>
    若 b = 0,则必定满足 a = 1,即:<b,0,a,1>
    若 a = 1,则必定满足 b = 0,即:<a,1,b,0>
    若 b = 1,则必定满足 a = 0,即:<b,1,a,0>

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=10007;
const int N=1000000+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;bool vis[N*2];
int Stack[N*2],top;
vector<int> G[N*2];
void init(int n){memset(vis,false,sizeof(vis));for(int i=0;i<2*n;i++)G[i].clear();
}
void addOrClause(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);
}
void addAndClause(int x,int xval,int y,int yval) {x=x*2+xval;y=y*2+yval;G[x].push_back(y);
}
bool dfs(int x){if(vis[x^1])return false;if(vis[x])return true;vis[x]=true;Stack[top++]=x;for(int i=0;i<G[x].size();i++)if(!dfs(G[x][i]))return false;return true;
}
bool twoSAT(int n){for(int i=0;i<2*n;i+=2){if(!vis[i] && !vis[i+1]){top=0;if(!dfs(i)){while(top>0)vis[Stack[--top]]=false;if(!dfs(i+1))return false;}}}return true;
}
int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){init(n);while(m--){int a,b,c;char ch[10];scanf("%d%d%d%s",&a,&b,&c,ch);if(ch[0]=='A'){//and 运算if(c==0){//a and b=0addAndClause(a,1,b,0);//若 a=1,则必定满足 b=0addAndClause(b,1,a,0);//若 b=1,则必定满足 a=0}else{//a and b=1addAndClause(a,0,a,1);//a=1addAndClause(b,0,b,1);//b=1}}else if(ch[0]=='O'){//or 运算if(c==0){//a or b=0addAndClause(a,1,a,0);//a=0addAndClause(b,1,b,0);//b=0}else{//a or b=1addAndClause(a,0,b,1);//若 a=0,则必定满足 b=1addAndClause(b,0,a,1);//若 b=0,则必定满足 a=0}}else if(ch[0]=='X'){//xor 运算if(c==0){//a xor b=0addAndClause(a,0,b,0);//若 a = 0,则必定满足 b = 0addAndClause(b,0,a,0);//若 b = 0,则必定满足 a = 0addAndClause(a,1,b,1);//若 a = 1,则必定满足 b = 1addAndClause(b,1,a,1);//若 b = 1,则必定满足 a = 1}else{//a xor b=1addAndClause(a,0,b,1);//若 a = 0,则必定满足 b = 1addAndClause(b,0,a,1);//若 b = 0,则必定满足 a = 1addAndClause(a,1,b,0);//若 a = 1,则必定满足 b = 0addAndClause(b,1,a,0);//若 b = 1,则必定满足 a = 0}}}printf("%s\n",twoSAT(n)?"YES":"NO");}return 0;
}

Katu Puzzle(POJ-3678)相关推荐

  1. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  2. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  3. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  4. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  5. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  6. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  7. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  8. puzzle(004.1)日历拼图

    目录 一,规则 二,每日拼图 2022年2月 2022年3月 竖条下滑问题 2022年4月 2022年5月 2022年6月 三,术语 四,启发式搜索策略 1,数独 2,策略一 3,策略二 4,策略三 ...

  9. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

最新文章

  1. TensorFlow优化器及用法
  2. 使用Python,OpenCV和Hough圆检测图像中的圆
  3. getElementsByName和getElementById获取控件
  4. Xamarin Android权限请求
  5. plsql 查询存储过程死锁语句_SQL2005查看死锁存储过程sp_who_lock
  6. (干货)css常用技巧
  7. Linux下修改系统时间并写入BIOS
  8. 矩阵横向输出_Python3算法之八:矩阵螺旋遍历
  9. 51单片机十字交通灯程序设计
  10. 今日download工程的奇怪问题error C2039: 'readStdErr' : is not a member of 'Programer',
  11. 拟合数据和原始数据误差计算
  12. 写个单机版斗地主程序,复习c++面向对象
  13. 《投资中最简单的事》”第二部分--投资办法“读书笔记
  14. 青海电大随学随考计算机,[青海电大]17秋随学随考中国现当代文学名著导读(1)作业4资料...
  15. win10高危服务_您可以安全地禁用哪些Win10服务?
  16. winDebug 调试
  17. Direct3D中的光照
  18. Cron表达式详解和表达式的验证
  19. 这波分享得你们都爱了吗?
  20. 光脚丫思考Vue3与实战:第05章 计算属性和侦听器 第02节 侦听器

热门文章

  1. 宣战抖音!腾讯与头条之战的新局面与猜想
  2. 脉冲波形的变换与产生
  3. 求你了,听我一句劝吧,这几个玩意就别学了!
  4. 知乎:fastjson这么快,为啥老外还是热衷 jackson?
  5. 所有和Java中代理有关的知识点都在这了。
  6. 第一批90后已经30岁了,更扎心的是…
  7. 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法
  8. 国外著名java技术资料网站
  9. 【MATLAB】xx操作总结【更新中】
  10. 医学图像数据集和处理工具【总结】