POJ3678 Katu Puzzle 【2-sat】
题目
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.
输入格式
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 a line containing "YES" or "NO".
输入样例
4 4
0 1 1 AND
1 2 1 OR
3 2 0 AND
3 0 0 XOR
输出样例
YES
提示
X0 = 1, X1 = 1, X2 = 0, X3 = 1.
题解
跪了。。。就因为n << 1写成了1 << n QAQ
这题加深了我对2-sat建图的理解,建边就表示选择了起点就必须选择终点
对于每个限制条件,我们分别考虑选择x的不同值
AND
为1,则x0->x1,y0->y1,让x0,y0自相矛盾,无法选择
为0,则x0->y1,y0->x1
OR
为1,则x0->y1,y0->x1
为0,则x1->x0,y1->y0
XOR
为1,则x0->y1,x1->y0,y1->x0,y0->x1
为0,则x0->y0,x1->y1,y0->x0,y1->x1
tarjan判断一下x0和x1是否在同一个强联通分量即可
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(x) memset(x,0,sizeof(x))
using namespace std;
const int maxn = 4005,maxm = 4000005,INF = 1000000000;
int n,m,h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
void build(int u,int v){ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;}
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],scci,top,cnt;
void dfs(int u){
	dfn[u] = low[u] = ++cnt;
	st[++top] = u;
	Redge(u){
		if (!dfn[to = ed[k].to]){
			dfs(to);
			low[u] = min(low[u],low[to]);
		}else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
	}
	if (dfn[u] == low[u]){
		scci++;
		do{Scc[st[top]] = scci;}while (st[top--] != u);
	}
}
char opt[10];
int main(){
	while (~scanf("%d%d",&n,&m)){
		int a,b,v,x0,x1,y0,y1; cnt = scci = top = 0; ne = 1;
		cls(dfn); cls(h); cls(Scc); cls(low);
		while (m--){
			scanf("%d%d%d%s",&a,&b,&v,opt);
			x0 = a << 1; x1 = x0 | 1; y0 = b << 1; y1 = y0 | 1;
			if (opt[0] == 'A'){
				if (v) build(x0,x1),build(y0,y1);
				else build(x1,y0),build(y1,x0);
			}
			else if (opt[0] == 'O'){
				if (v) build(x0,y1),build(y0,x1);
				else build(x1,x0),build(y1,y0);
			}
			else if (opt[0] == 'X'){
				if (v) build(x0,y1),build(y0,x1),build(x1,y0),build(y1,x0);
				else build(x1,y1),build(y0,x0),build(x0,y0),build(y1,x1);
			}
		}
		for (int i = 0; i < 2 * n; i++) if (!dfn[i]) dfs(i);
		bool flag = true;
		for (int i = 0; i < n; i++)
			if (Scc[i << 1] == Scc[i << 1 | 1]){
				flag = false; break;
			}
		if (flag) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}
												
											POJ3678 Katu Puzzle 【2-sat】的更多相关文章
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
		
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
 - poj3678 Katu Puzzle 2-SAT
		
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6714 Accepted: 2472 Descr ...
 - POJ-3678 Katu Puzzle 2sat
		
题目链接:http://poj.org/problem?id=3678 分别对and,or,xor推出相对应的逻辑关系: 逻辑关系 1 0 A and B A'->A,B'->B ...
 - POJ3678 Katu Puzzle
		
原题链接 \(2-SAT\)模板题. 将\(AND,OR,XOR\)转换成\(2-SAT\)的命题形式连边,用\(tarjan\)求强连通分量并检验即可. #include<cstdio> ...
 - POJ1651 Multiplication Puzzle【区间DP】
		
LINK 每次删除一个数,代价是左右两边相邻的数的当前数的积 第一个和最后一个数不能删除 问最后只剩下第一个数的最后一个数的最小代价 思路 很简单的DP 正着考虑没有办法确定两边的数 那么就把每个区间 ...
 - poj 1651 Multiplication Puzzle【区间DP】
		
题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...
 - USACO4.4 Shuttle Puzzle【bfs+优化】
		
直接上$bfs$,每一个状态记录下当前字符串的样子,空格的位置,和走到这个状态的答案. 用空格的位置转移,只有$50pts$ 考虑到题目一个性质:$W$只往右走,$B$只往左走,就可以过了. #inc ...
 - poj2893 M*N puzzle 【n*m数码问题小结】By cellur925
		
题目传送门 这个问题是来源于lydrainbowcat老师书上讲排序的一个扩展.当时讲的是奇数码问题,其实这种问题有两种问法:一种局面能否到另一种局面.到达目标局面的最小步数. 本文部分内容引用于ly ...
 - 【codeforces 761E】Dasha and Puzzle
		
[题目链接]:http://codeforces.com/contest/761/problem/E [题意] 给你一棵树,让你在平面上选定n个坐标; 使得这棵树的连接关系以二维坐标的形式展现出来; ...
 
随机推荐
- 使用FolderBrowserDialog组件选择文件夹
			
实现效果: 知识运用: FolderBrowserDialog组件的ShowDialog方法 //弹出选择路径对话框 public DialogResult ShowDialog() 和Selecte ...
 - Java环境变量搭建(Linux环境)
			
1. 下载解压JDK压缩包 例如:解压到 /opt/jdk1.7.0_80 下 2. 添加环境变量到 /etc/profile 文件中 vi /etc/profile 在文件末尾追加如下内容: exp ...
 - Debug与Release版本的区别详解
			
原文链接 Debug 和 Release 并没有本质的区别,他们只是VC预定义提供的两组编译选项的集合,编译器只是按照预定的选项行动.如果我们愿意,我们完全可以把Debug和Release的行为完全颠 ...
 - js常见问题总结归纳
			
一.使用 typeof bar === "object" 来确定 bar 是否是对象的潜在陷阱是什么?如何避免这个陷阱? 首先typeof bar === "object ...
 - 【Ecshop】修改处理用户购物车的行为
			
Ecshop v2.7.3的购物车处理方面在现在看来有比较反用户体验的设计: 用户未登录时加入购物车的商品,在用户登录后会被清空而不是加入到登录用户的购物车中: 用户登录后加入购物车的商品,在退出后会 ...
 - tp3.2读取time()格式遇到的的问题(尚未解决)
			
在用tp3.2框架做一个讲座模块.最近又遇到了一个问题 如上图所示,我把日期和讲座开始时间结束时间分来放了.(这里的Jdate2和jdate3本来存放为time(7)类型的,后发现在原来这个7是可以改 ...
 - pycharm配置Git托管
			
利用Pycharm和github管理代码转载https://www.cnblogs.com/feixuelove1009/p/5955332.html git教程--廖雪峰git教程 转载https ...
 - 三分钟明白 Activity工作流 -- java运用
			
一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假申请——上级口头同意——上级将请假记录下来——月底将请假记录上交公司——公司将请假录入电脑 采用工作 ...
 - 使用python实现滑动验证码
			
首先安装一个需要用到的模块 pip install social-auth-app-django 安装完后在终端输入pip list会看到 social-auth-app-django social- ...
 - 【STM32】IIC的基本原理(实例:普通IO口模拟IIC时序读取24C02)(转载)
			
版权声明:本文为博主原创文章,允许转载,但希望标注转载来源. https://blog.csdn.net/qq_38410730/article/details/80312357 IIC的基本介绍 ...