题目

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

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

  2. poj3678 Katu Puzzle 2-SAT

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6714   Accepted: 2472 Descr ...

  3. POJ-3678 Katu Puzzle 2sat

    题目链接:http://poj.org/problem?id=3678 分别对and,or,xor推出相对应的逻辑关系: 逻辑关系 1 0  A and B     A'->A,B'->B ...

  4. POJ3678 Katu Puzzle

    原题链接 \(2-SAT\)模板题. 将\(AND,OR,XOR\)转换成\(2-SAT\)的命题形式连边,用\(tarjan\)求强连通分量并检验即可. #include<cstdio> ...

  5. POJ1651 Multiplication Puzzle【区间DP】

    LINK 每次删除一个数,代价是左右两边相邻的数的当前数的积 第一个和最后一个数不能删除 问最后只剩下第一个数的最后一个数的最小代价 思路 很简单的DP 正着考虑没有办法确定两边的数 那么就把每个区间 ...

  6. poj 1651 Multiplication Puzzle【区间DP】

    题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...

  7. USACO4.4 Shuttle Puzzle【bfs+优化】

    直接上$bfs$,每一个状态记录下当前字符串的样子,空格的位置,和走到这个状态的答案. 用空格的位置转移,只有$50pts$ 考虑到题目一个性质:$W$只往右走,$B$只往左走,就可以过了. #inc ...

  8. poj2893 M*N puzzle 【n*m数码问题小结】By cellur925

    题目传送门 这个问题是来源于lydrainbowcat老师书上讲排序的一个扩展.当时讲的是奇数码问题,其实这种问题有两种问法:一种局面能否到另一种局面.到达目标局面的最小步数. 本文部分内容引用于ly ...

  9. 【codeforces 761E】Dasha and Puzzle

    [题目链接]:http://codeforces.com/contest/761/problem/E [题意] 给你一棵树,让你在平面上选定n个坐标; 使得这棵树的连接关系以二维坐标的形式展现出来; ...

随机推荐

  1. WARNING: The TCP backlog setting of 511.解决

    redis启动警告问题:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/so ...

  2. python之道12

    整理今天笔记,课上代码最少敲3遍. 用列表推导式做下列小题 过滤掉长度小于3的字符串列表,并将剩下的转换成大写字母 l = ['wusir', 'laonanhai', 'aa', 'b', 'tai ...

  3. sudo apt-get install ubuntu-desktop, Error: unable to locate package

    http://askubuntu.com/questions/130532/sudo-apt-get-install-ubuntu-desktop-error-unable-to-locate-pac ...

  4. qsort()和bsearch()

    qsort void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); Sort ...

  5. MyElipes遇到 source not found解决方案

    在用Myeclipse 或者是eclipse进行开发时候经常遇到这个问题. File class editor  source not found问题.原因很简单,就是因为这是一个源码包,相应的没有编 ...

  6. linux系统快捷键使用

    本文记录linux系统中快捷键的使用 Ctrl + l 清屏,相当于clear命令Ctrl + o 执行当前命令,并重新显示本命令Ctrl + s 阻止屏幕输出,锁定Ctrl + q 允许屏幕输出Ct ...

  7. yum安装报错

    检查了好久才知道原来是 sudo nano /etc/sysconfig/network-scripts/ifcfg-ens33 下的DNS配错了,改好之后,sudo service network ...

  8. vue.js 图表chart.js使用

    在使用这个chart.js之前,自己写过一个饼图,总之碰到的问题不少,所以能用现成的插件就用,能节省不少时间 这里不打算介绍chart.js里面详细的参数意义和各个参数的用法,只作为首次使用chart ...

  9. SSM(Spring+Spring MVC+Mybatis)开发前台后功能完整的java开源博客管理系统

    项目描述 本项目通过SSM(SpringMVC+Mybatis+Spring)框架编写的一个人博客管理系统,使用hexo主题,以及MAVEN进行对项目管理,并且前端具有粒子和点击爱心效果.后端的页面框 ...

  10. North American Invitational Programming Contest (NAIPC) 2016

    (待补) A. Fancy Antiques 爆搜. B. Alternative Bracket Notation C. Greetings! D. Programming Team 0/1分数规划 ...