Katu Puzzle POJ - 3678 (2 - sat)
有N个变量X1X1~XNXN,每个变量的可能取值为0或1。
给定M个算式,每个算式形如 XaopXb=cXaopXb=c,其中 a,b 是变量编号,c 是数字0或1,op 是 and,or,xor 三个位运算之一。
求是否存在对每个变量的合法赋值,使所有算式都成立。
输入格式
第一行包含两个整数N和M。
接下来M行,每行包含三个整数a b c,以及一个位运算(AND,OR,XOR中的一个)。
输出格式
输出结果,如果存在,输出“YES”,否则输出“NO”
数据范围
1≤N≤10001≤N≤1000,
1≤M≤1061≤M≤106
输入样例:
4 4
0 1 1 AND
1 2 1 OR
3 2 0 AND
3 0 0 XOR
输出样例:
YES
思路:根据题意,该题是一个 2-SAT 问题,将操作符进行转换,进行判定即可
对于 A[x],可以通过连边 <x',x> 实现,NOT A[x],可以通过连边 <x,x'> 来实现,对于 NOT(A[x] AND A[y]) 需要连两条边 <x, y'> 和 <y, x'> 来实现,对于 A[x] OR A[y] 需要连两条边 <x', y> 和 <y', x> 来实现
故对于 and、or、xor 三种运算有:
and 运算:
a and b = 0 时:若 a=1,则必定满足 b=0;若 b=1,则必定满足 a=0,即:<a,1,b,0>、<b,1,a,0>
a and b = 1 时:a=1 且 b=1,即:<a,0,a,1>、<b,0,b,1>
or 运算:
a or b = 0 时:a = 0 且 b = 0,即:<a,1,a,0>、<b,1,b,0>
a or b = 1 时:若 a=0,则必定满足 b=1;若 b=0,则必定满足 a=0,即:<a,0,b,1>、<b,0,a,1>
xor 运算:
a xor b = 0 时,有以下四种情况:
若 a = 0,则必定满足 b = 0,即:<a,0,b,0>
若 b = 0,则必定满足 a = 0,即:<b,0,a,0>
若 a = 1,则必定满足 b = 1,即:<a,1,b,1>
若 b = 1,则必定满足 a = 1,即:<b,1,a,1>
a xor b = 1 时,有以下四种情况:
若 a = 0,则必定满足 b = 1,即:<a,0,b,1>
若 b = 0,则必定满足 a = 1,即:<b,0,a,1>
若 a = 1,则必定满足 b = 0,即:<a,1,b,0>
若 b = 1,则必定满足 a = 0,即:<b,1,a,0>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std;
#define debug(x) cout << "fuck bug " << x << "\n";
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int maxn = 4e5 + ;
typedef long long ll; int n,m; struct edge
{
int to,nxt;
}e[maxn]; int head[maxn],tot;
void add(int u ,int v){
e[++tot].to = v;
e[tot].nxt = head[u];
head[u] = tot;
} int dfn[maxn],low[maxn],num,inStack[maxn];
int stack[maxn],top,cnt,C[maxn];
void tarjan(int x) {
dfn[x] = low[x] = ++num;
stack[++top] = x; inStack[x] = true;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (dfn[y] == ) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if (inStack[y]) {
low[x] = min(low[x], low[y]);
}
}
if (low[x] == dfn[x]) {
cnt++;
int z;
do {
z = stack[top--];
inStack[z] = false;
C[z] = cnt;
} while (z != x);
}
} int main(int argc, char const *argv[])
{
//cin >> n >> m;
scanf("%d %d",&n,&m);
for(int i = ;i <= m ; i ++){
int a , b , c ; char str[];
//cin >> a >> b >> c >> str;
scanf("%d %d %d %s",&a,&b,&c,str);
if(str[] == 'A'){
if(c == ){
add(a,a+n);
add(b,b+n);
}else{
add(a+n,b);
add(b+n,a);
}
}else if(str[] == 'O'){
if(c == ){
add(a,b+n);
add(b,a+n);
}else{
add(a+n,a);
add(b+n,b);
}
}else if(str[] == 'X'){
if(c == ){
add(a,b+n);
add(b,a+n);
add(a+n,b);
add(b+n,a);
}else{
add(a,b);
add(b,a);
add(a+n,b+n);
add(b+n,a+n);
}
}
} for(int i = ;i < n + n; i++){
if(!dfn[i]) tarjan(i);
} bool flag=;
for(int i = ;i < n ;i ++){
if(C[i] == C[i + n]) {
flag = ;
break;
}
} if(flag) puts("YES");
else puts("NO"); return ;
}
Katu Puzzle POJ - 3678 (2 - sat)的更多相关文章
- Katu Puzzle POJ - 3678(水2 - sat)
题意: 有n个未知量,m对未知量之间的关系,判断是否能求出所有的未知量且满足这些关系 解析: 关系建边就好了 #include <iostream> #include <cstdio ...
- M × N Puzzle POJ - 2893(奇数码)
The Eight Puzzle, among other sliding-tile puzzles, is one of the famous problems in artificial inte ...
- 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 ...
- 学习笔记(two sat)
关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...
- poj 3335(半平面交)
链接:http://poj.org/problem?id=3335 //大牛们常说的测模板题 ------------------------------------------------- ...
- poj 3122 (二分查找)
链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- POJ——3169Layout(差分约束)
POJ——3169Layout Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14702 Accepted ...
- POJ 3252 (数位DP)
###POJ 3252 题目链接 ### 题目大意:给你一段区间 [Start,Finish] ,在这段区间中有多少个数的二进制表示下,0 的个数 大于等于 1 的个数. 分析: 1.很显然是数位DP ...
随机推荐
- 深入解析 composer 的自动加载原理
PHP 自5.3的版本之后,已经重焕新生,命名空间.性状(trait).闭包.接口.PSR 规范.以及 composer 的出现已经让 PHP 变成了一门现代化的脚本语言.PHP 的生态系统也一直在演 ...
- Notepad++格式化xml(转)
转自:http://www.herongyang.com/XML/NPP-XML-Tools-Plugin-Download-and-Install.html Downloading and inst ...
- mysql数据库引擎——MyISAM,InnoDB
作为一个java web开发人员,对于mysql数据库掌握到具体比较这两类引擎的差异也蛮拼的,下面就介绍一下我在工作中积累的对这两类引擎的理解. MyISAM: 如果不更改mysql配置文件(my.i ...
- python之scrapy爬取数据保存到mysql数据库
1.创建工程 scrapy startproject tencent 2.创建项目 scrapy genspider mahuateng 3.既然保存到数据库,自然要安装pymsql pip inst ...
- [git]使用Idea创建一个git项目
第一次使用git的方法,如建立的项目名叫:my-webapp 第一步:在远程gitlab上建立空白项目:my-webapp 第二步:在本地建立项目my-webapp,添加代码 第三步:创建一个本地 ...
- 自定义zabbix脚本--网卡平均流量
自定义zabbix脚本--网卡平均流量1. 在客户端修改配置文件 /etc/zabbix/zabbix_agentd.conf需要改动两个地方:(1) UnsafeUserParameters=1(2 ...
- ajax基础------备忘
1:register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- jmeter 执行python脚本的方法 。(亲测ok)
jmeter 执行python脚本 jmeter 可以通过Jython 执:行python代码 1.下载Jython jar包:http://www.jython.org/downloads.ht ...
- mysql安装报错error: Header V3 DSA signature: BAD, key ID
CentOS安装rpm安装MySQL时爆出警告: warning: mysql-community-server-5.7.18-1.el6.x86_64.rpm: Header V3 DSA/SHA1 ...
- 简单wait(),notify()方法
1.两个类public class Name{ public static void main(String[] args) throws InterruptedException { User us ...