POJ 3678 Katu Puzzle
来源:互联网 发布:wine源码 编辑:程序博客网 时间:2024/06/05 01:41
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.
【题目分析】
其实暴搜加上适当的剪枝可以0ms的,但是熟悉一下2set,建图的方式非常巧妙。
【代码】
#include <cstdio>#include <vector>using namespace std;typedef long long ll;int dfn[2001],vis[2001],low[2001],sta[2001*10],bel[2001],tem,cnt,top;vector<int>e[2001];void tarjan(int u){ dfn[u]=low[u]=++tem; vis[u]=true; sta[++top]=u; int v,i,l=e[u].size(); for(i=0;i<l;i++) { v=e[u][i]; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(vis[v]&&dfn[v]<low[u]) low[u]=dfn[v]; } if(dfn[u]==low[u]) { cnt++; do { v=sta[top--]; vis[v]=false; bel[v]=cnt; }while(v!=u); }}bool twoSAT(int n){ for(int i=0;i<2*n;i++) if(!dfn[i]) tarjan(i); for(int i=0;i<n;i++) if(bel[2*i]==bel[2*i+1]) return false; return true;}int main(){ int n,m,i,a,b,c; char s[5]; scanf("%d%d",&n,&m); for(i=0;i<m;i++) { scanf("%d%d%d%s",&a,&b,&c,s); if(s[0]=='A') { if(c) e[2*a+1].push_back(2*a),e[2*b+1].push_back(2*b); else e[2*a].push_back(2*b+1),e[2*b].push_back(2*a+1); } else if(s[0]=='O') { if(c) e[2*a+1].push_back(2*b),e[2*b+1].push_back(2*a); else e[2*a].push_back(2*a+1),e[2*b].push_back(2*b+1); } } if(twoSAT(n)) puts("YES"); else puts("NO"); return 0;}
0 0
- POJ 3678 Katu Puzzle
- poj 3678 Katu Puzzle
- poj 3678 Katu Puzzle
- POJ 3678 Katu Puzzle
- POJ 3678 Katu Puzzle
- poj 3678 Katu Puzzle
- POJ 3678 Katu Puzzle
- 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)
- 打印蛇形矩阵
- 【旧代码整理】uri路由处理部分
- 面试题汇总
- apply函数处理数据的方式
- 私有网络地址
- POJ 3678 Katu Puzzle
- 2016夏季练习——二分
- 设备节点和设备堆栈
- 购物车与商城订单的关系以及技术实现深入分析
- linux关闭按键报警声
- android textview 添加上划线 中划线 删除线
- 图优化理论与g2o的使用(1)
- Qt申请窗口类对象,构造函数无法解析
- Win8.1、Win10安装msi程序报2052、2053错误的解决方法