poj 3678 Katu Puzzle(2-sat应用)
来源:互联网 发布:制作生日快乐贺卡软件 编辑:程序博客网 时间:2024/06/08 06:43
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:
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
题目:http://poj.org/problem?id=3678
题意:给你n个布尔型变量,给你一个关系表,标志n个变量之间的关系,问这样的关系是否合法。。。。
分析:好吧,由于是在2-sat分类里面看到的题,所有直接想到2-sat,只要分清3个关系式,就可以建图了,比如and并且为1,则两个变量同时为1,and为0的话,只要保证一个是0就行。。。
代码:
#include<cstdio>#include<iostream>using namespace std;const int mm=4444444;const int mn=2222;int ver[mm],next[mm];int head[mn],dfn[mn],low[mn],q[mn],id[mn];int i,j,k,n,m,idx,top,cnt,edge;void add(int u,int v){ ver[edge]=v,next[edge]=head[u],head[u]=edge++;}void dfs(int u){ dfn[u]=low[u]=++idx; q[top++]=u; for(int i=head[u],v;i>=0;i=next[i]) if(!dfn[v=ver[i]]) dfs(v),low[u]=min(low[u],low[v]); else if(!id[v])low[u]=min(low[u],dfn[v]); if(dfn[u]==low[u]) { id[u]=++cnt; while(q[--top]!=u)id[q[top]]=cnt; }}void Tarjan(){ for(idx=top=cnt=i=0;i<n+n;++i)dfn[i]=id[i]=0; for(i=0;i<n+n;++i) if(!dfn[i])dfs(i);}bool ok(){ for(i=0;i<n+n;i+=2) if(id[i]==id[i^1])return 0; return 1;}char op[22];int main(){ while(~scanf("%d%d",&n,&m)) { for(edge=i=0;i<n+n;++i)head[i]=-1; while(m--) { scanf("%d%d%d%s",&i,&j,&k,op); if(op[0]=='A') { if(k)add(i<<1,i<<1|1),add(j<<1,j<<1|1); else add(i<<1|1,j<<1),add(j<<1|1,i<<1); } if(op[0]=='O') { if(k)add(i<<1,j<<1|1),add(j<<1,i<<1|1); else add(i<<1|1,i<<1),add(j<<1|1,j<<1); } if(op[0]=='X') { if(k)add(i<<1|1,j<<1),add(j<<1|1,i<<1),add(i<<1,j<<1|1),add(j<<1,i<<1|1); else add(i<<1,j<<1),add(j<<1,i<<1),add(i<<1|1,j<<1|1),add(j<<1|1,i<<1|1); } } Tarjan(); puts(ok()?"YES":"NO"); } return 0;}
- 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判断)
- 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)
- <4>c语言实现ATM存取款系统(链表)
- 2011年北京区域赛I-Activation
- 热门的网站测试工具
- 基于v4l2 ffmpeg x264的视频远程监控
- hdu 1242 &&zoj 1649 Rescue
- poj 3678 Katu Puzzle(2-sat应用)
- fatal:no bootable medium found!system halted
- hdu 1003 Max Sum 最大连续子串和
- poj 3460 Booksort(IDA*)
- 思维导图
- 初识异常处理
- 全排列
- 智能算法之微分进化算法(差分进化算法)
- server_fork.c