有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)的更多相关文章

  1. Katu Puzzle POJ - 3678(水2 - sat)

    题意: 有n个未知量,m对未知量之间的关系,判断是否能求出所有的未知量且满足这些关系 解析: 关系建边就好了 #include <iostream> #include <cstdio ...

  2. M × N Puzzle POJ - 2893(奇数码)

    The Eight Puzzle, among other sliding-tile puzzles, is one of the famous problems in artificial inte ...

  3. 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 ...

  4. 学习笔记(two sat)

    关于two sat算法 两篇很好的论文由对称性解2-SAT问题(伍昱), 赵爽 2-sat解法浅析(pdf). 一些题目的题解 poj 3207 poj 3678 poj 3683 poj 3648 ...

  5. poj 3335(半平面交)

    链接:http://poj.org/problem?id=3335     //大牛们常说的测模板题 ------------------------------------------------- ...

  6. poj 3122 (二分查找)

    链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  7. poj3270 && poj 1026(置换问题)

    | 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...

  8. POJ——3169Layout(差分约束)

    POJ——3169Layout Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14702   Accepted ...

  9. POJ 3252 (数位DP)

    ###POJ 3252 题目链接 ### 题目大意:给你一段区间 [Start,Finish] ,在这段区间中有多少个数的二进制表示下,0 的个数 大于等于 1 的个数. 分析: 1.很显然是数位DP ...

随机推荐

  1. 0 - Visualizing and Understanding Convolutional Networks(阅读翻译)

    卷积神经网络的可视化理解(Visualizing and Understanding Convolutional Networks) 摘要(Abstract) 近来,大型的卷积神经网络模型在Image ...

  2. 整合pjax无刷新

    一:整合pjax的准备工作: 检查你的网站是否引入1.7.0版本以上的jquery.js,如果没有请全局引入 1.新浪CDN提速:<script type="text/javascri ...

  3. 谷歌guava缓存

    简易缓存,可以设置时间的缓存 private static Cache<String,String> tokenCache = CacheBuilder.newBuilder().expi ...

  4. centos系统python2.7更新到3.5

    1. 下载Python-3.5.2 wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz 2.安装 (报错no acceptabl ...

  5. 提高组刷题营 DAY 2

    1.滞空(jump/1s/64M) #include<bits/stdc++.h> using namespace std; typedef long long LL; ; inline ...

  6. Android View的加载流程

    什么是Activity? Activity是 用户操作的可视化界面:它为用户提供了一个放置视图和交互操作的窗口.采用setContentView的方法提供.因此,可以理解Activity.Window ...

  7. 本地服务CURL请求本地另一个服务API返回超时/或无返回

    入职之后一直在忙,终于有时间整理一波最近踩到的坑. 起因: 项目是微服务架构,一个项目对外提供API,新的项目调用API获得数据.于是就在本地搭建了两个服务.配置了两个虚拟域名,指向两个项目,当然我本 ...

  8. ctf密码学------密文解码python脚本(凯撒解密)

    题目来源实验吧 分析题意,说是困在栅栏中,所以将字符栅栏解密看看有什么,利用工具CTFcraktools 得到三条密文 然后说是密码是凯撒,在将四栏依次凯撒解码,寻找可能的key,这里很显然,在尝试第 ...

  9. Oracle事务、视图、序列

    回顾什么是事务? 一个不可分割的子操作形成一个整体,该整体要么全部执行成功,要么全部执行失败.例如:转帐 回顾为什么要用事务? 如果不用事务的话,为转帐为例,可能出现一个用户钱增加了,另一个用户钱不变 ...

  10. php代码判断用户访问的当前协议是否为https

    public function isHttps() { if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) ...