poj3678Katu Puzzle
来源:互联网 发布:云腾网络加速器 编辑:程序博客网 时间:2024/06/05 04:59
#include<iostream>#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<algorithm>using namespace std;const int MAXN = 1010; struct twosat { int n,c; vector<int> g[MAXN<<1]; bool mark[MAXN<<1]; int s[MAXN<<1]; bool dfs(int x) { int i; if (mark[x^1]) return 0; if (mark[x]) return 1; mark[x] = 1; s[c++] = x; for (i = 0; i < g[x].size(); i++) if (!dfs(g[x][i])) return 0; return 1; } void init(int n) { int i,t=n<<1; this->n = n; for (i = 0; i < t; i++) g[i].clear(); memset(mark, 0, sizeof(mark)); } void add_clause(int x,int xval,int y,int yval) { x=x*2+xval; y=y*2+yval; g[x].push_back(y^1); g[y].push_back(x^1); } bool solve() { int i,t=n<<1; for (i = 0; i < t; i += 2) if (!mark[i] && !mark[i + 1]) { c = 0; if (!dfs(i)) { while (c > 0) mark[s[--c]] =0; if (!dfs(i + 1)) return 0; } } return 1; } }ender;int work(int a,int b,int k){return k==0?a&b:k==1?a|b:a^b;}int main(){char s[100];int a,b,c,n,m,x,y,k;while(cin>>n>>m){ender.init(n);while(m--){scanf("%d%d%d%s",&a,&b,&c,s);k=strcmp(s,"AND")==0?0:strcmp(s,"OR")==0?1:2;for(x=0;x<2;x++)for(y=0;y<2;y++)if(work(x,y,k)!=c)ender.add_clause(a,x,b,y);}cout<<(ender.solve()?"YES":"NO")<<endl;}return 0;}
Katu Puzzle
Description
Katu Puzzle is presented as a directed graph G(V, E) with each edgee(a, b) labeled by a boolean operator op (one of AND, OR, XOR) and an integerc (0 ≤ c ≤ 1). One Katu is solvable if one can find each vertexVi a value Xi (0 ≤ Xi ≤ 1) such that for each edgee(a, b) labeled by op and c, the following formula holds:
Xa op Xb = c
The calculating rules are:
AND01000101OR01001111XOR01001110Given 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 40 1 1 AND1 2 1 OR3 2 0 AND3 0 0 XOR
Sample Output
YES
Hint
Source
- poj3678Katu Puzzle
- poj3678Katu Puzzle
- poj3678Katu Puzzle【2-SAT】
- poj3678Katu Puzzle(2sat入门)
- puzzle
- puzzle
- puzzle
- Puzzle
- Puzzle
- Puzzle
- puzzle
- OO Puzzle
- Java Puzzle
- Tropical Puzzle
- Google puzzle
- multiplication puzzle
- Inherinates Puzzle
- Multiplication Puzzle
- 编写函数计算圆的面积,参数是圆的半径。在main函数中测试。
- POJ 题目1101 The Game(bfs)
- 第14周项目2-带姓名的成绩单
- 下载android5.0源码
- fssh--闪速批量远程命令执行利器源码发布啦
- poj3678Katu Puzzle
- 学习笔记之Hibernate
- 《网络协议》图解 TCP 连接建立与释放
- 前端代码标准最佳实践:javascript篇
- SOPAS通信文档
- 常用网络工具sendip参数说明
- ZigBee组网学习笔记(一)无线点灯
- TCP 的那些事儿
- IOS学习博客-坚持就是胜利