POJ 3678 Katu Puzzle (2-sat,4级)
来源:互联网 发布:mac口红吧怎么看真假 编辑:程序博客网 时间:2024/05/24 05:58
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
#include<cstdio>#include<cstring>#include<iostream>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=2010;class Edge{ public:int v,next;};class TWO_SAT{public: int dfn[mm],e_to[mm],stack[mm]; Edge e[mm*mm*2]; int edge,head[mm],top,dfs_clock,bcc; void clear() { edge=0;clr(head,-1); } void add(int u,int v) { e[edge].v=v;e[edge].next=head[u];head[u]=edge++; } void add_my(int x,int xval,int y,int yval) { x=x+x+xval;y=y+y+yval; add(x,y); } void add_clause(int x,int xval,int y,int yval) {///x or y x=x+x+xval; y=y+y+yval; add(x^1,y);add(y^1,x); } void add_con(int x,int xval)//x is xval { x=x+x+xval; add(x^1,x); } int tarjan(int u) { int lowu,lowv; lowu=dfn[u]=++dfs_clock; int v; stack[top++]=u; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(!dfn[v]) { lowv=tarjan(v); lowu=min(lowv,lowu); } else if(e_to[v]==-1)//in stack lowu=min(lowu,dfn[v]); } if(dfn[u]==lowu) { ++bcc; do{ v=stack[--top]; e_to[v]=bcc; }while(v!=u); } return lowu; } bool find_bcc(int n) { clr(e_to,-1); clr(dfn,0); bcc=dfs_clock=top=0; FOR(i,0,2*n-1) if(!dfn[i]) tarjan(i); for(int i=0;i<2*n;i+=2) if(e_to[i]==e_to[i^1])return 0; return 1; }}two;int n,m;char s[44];int main(){ int a,b,c,d; while(~scanf("%d%d",&n,&m)) { two.clear(); FOR(i,1,m) { scanf("%d%d%d%s",&a,&b,&c,s); if(s[0]=='A') { if(c) { two.add_clause(a,1,b,1); two.add_clause(a,0,b,1);//imposible a 1 b 0 two.add_clause(a,1,b,0);// two.add_my(a,0,b,1);// two.add_my(a,0,b,0);// two.add_my(b,0,a,1);// two.add_my(b,0,a,0); } else { two.add_clause(a,0,b,0); } } else if(s[0]=='O') { if(c) { two.add_clause(a,1,b,1); } else { two.add_clause(a,0,b,0); two.add_clause(a,1,b,0); two.add_clause(a,0,b,1);// two.add_my(a,1,b,0);// two.add_my(a,1,b,1);// two.add_my(b,1,a,0);// two.add_my(b,1,a,1); } } else if(s[0]=='X') { if(c) { two.add_clause(a,1,b,1); two.add_clause(a,0,b,0); } else { two.add_clause(a,1,b,0); two.add_clause(a,0,b,1); } } } if(two.find_bcc(n*2))printf("YES\n"); else printf("NO\n"); } return 0;}
- POJ 3678 Katu Puzzle (2-sat,4级)
- POJ 3678 Katu Puzzle(2-sat)
- POJ 3678 Katu Puzzle(2-SAT)
- POJ 3678 Katu Puzzle(2-SAT)
- POJ 3678 Katu Puzzle (2-SAT)
- poj 3678 Katu Puzzle (2-SAT)
- poj 3678 Katu Puzzle(2-sat)
- POJ 3678 Katu Puzzle (2-SAT)
- POJ 3678 Katu Puzzle(2-SAT判断)
- poj 3678 Katu Puzzle 2-SAT
- 2-sat->poj 3678 Katu Puzzle
- poj 3678 Katu Puzzle (2-sat)
- POJ 3678 Katu Puzzle(2-SAT)
- poj 3678 2-SAT (Katu Puzzle)
- 【POJ】3678 Katu Puzzle 2-sat
- [2-SAT] poj 3678 Katu Puzzle
- POJ 3678 Katu Puzzle(2-sat)
- POJ 3678 Katu Puzzle (2-SAT)
- 浮点数的二进制表示
- linux zip压缩命令解压命令
- 单调队列-hdu-3415-Max Sum of Max-K-sub-sequence
- [各种面试题] 将一棵普通二叉树转换为一棵线索二叉树
- Java核心技术,让计算机"一芯多用"的多线程技术
- POJ 3678 Katu Puzzle (2-sat,4级)
- 俞敏洪+马云+牛根生+史玉柱经典语录
- Ubuntu下Samba服务器的搭建
- 一天之内可以完成的事
- 如何合并apk和odex文件
- poj 1696 Space Ant(计算几何)
- HDU 3622 Bomb Game (二分+2-sat,4级 )
- EZ-USB开发笔记(三)——内存安排
- 四位计算机的原理及其实现