Katu Puzzle

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6714   Accepted: 2472

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.

Input

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

Output a line containing "YES" or "NO".

Sample Input

4 4
0 1 1 AND
1 2 1 OR
3 2 0 AND
3 0 0 XOR

Sample Output

YES

Hint

X0 = 1, X1 = 1, X2 = 0, X3 = 1.

Source

 
先进性tarjan算法找出强连通分量,判断每个点的取值是否矛盾,矛盾即为对于题意中每个点的0,1是否存在于一个强连通分量中,矛盾输出NO,不矛盾输出1;
建图规则:
a & b = 1 等价于 !a->a, !b->b;(旨在使得a或b取值为0是产生矛盾)
a & b = 0 等价于 a->!b, b->!a;
a | b = 1  等价于 !a->b, !b->a;
a | b = 0  等价于 a->!a, b->!b;
a ^ b = 1 等价于 !a->b, !b->a, a->!b, b->!a;
a ^ b = 0 等价于 a->b, b->a, !a->!b, !b->!a;  
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 1005 * 2;
int n,m;
int head[MAX];
struct node {
int t,next;
}edge[4000000];
int cnt;
void add(int u, int v) {
edge[cnt].t = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int bfn[MAX];
int low[MAX];
int vis[MAX];
int s[MAX];
int sn;
void tarbfs(int u, int lay, int & scc_num) {
vis[u] = 1;
bfn[u] = low[u] = lay;
s[sn++] = u;
int i;
for (i = head[u]; i != -1; i = edge[i].next) {
int tmp = edge[i].t;
if (!vis[tmp])tarbfs(tmp, ++lay, scc_num);
if (vis[tmp] == 1)low[u] = min(low[u], low[tmp]);
}
if (low[u] == bfn[u]) {
scc_num++;
while (1) {
sn--;
vis[s[sn]] = 2;
low[s[sn]] = scc_num;
if (s[sn] == u)break;
}
}
}
int tarjan() {
int scc_num = 0;
int lay = 1;
int i;
sn = 0;
memset(vis, 0, sizeof(vis));
for (i = 0; i < n; i++) {
if (!vis[i])
tarbfs(i, lay, scc_num);
}
return scc_num;
} int main() {
// freopen("in.txt","r",stdin);
int a,b,c;
char ch[5];
int i;
while (scanf("%d %d",&n,&m) != EOF) {
memset(head, -1, sizeof(head));
n = 2 * n;
cnt = 0;
for (i = 0; i < m; i++) {
scanf("%d %d %d %s",&a,&b,&c,ch);
if (ch[0] == 'A') {
if (c == 1) {
add(a<<1, a<<1|1);
add(b<<1, b<<1|1);
} else {
add(a<<1|1, b<<1);
add(b<<1|1, a<<1);
}
} else if (ch[0] == 'O') {
if (c == 1) {
add(a<<1, b<<1|1);
add(b<<1, a<<1|1);
} else {
add(a<<1|1, a<<1);
add(b<<1|1, b<<1);
}
} else {
if (c == 1) {
add(a<<1|1, b<<1);
add(b<<1|1, a<<1);
add(a<<1, b<<1|1);
add(b<<1, a<<1|1);
} else {
add(a<<1|1, b<<1|1);
add(b<<1|1, a<<1|1);
add(a<<1, b<<1);
add(b<<1, a<<1);
}
}
}
tarjan();
int flag = 0;
for (i = 0; i < n / 2; i++) {
if (low[i<<1] == low[i<<1|1]) {
flag = 1;
break;
}
}
if (flag)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}

  

poj3678 Katu Puzzle 2-SAT的更多相关文章

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

  2. POJ-3678 Katu Puzzle 2sat

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

  3. poj 3678 Katu Puzzle(Two Sat)

    题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...

  4. POJ3678 Katu Puzzle

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

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

  6. poj 3678 Katu Puzzle(2-sat)

    Description Katu Puzzle ≤ c ≤ ). One Katu ≤ Xi ≤ ) such that for each edge e(a, b) labeled by op and ...

  7. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

  8. POJ 3678 Katu Puzzle (经典2-Sat)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 2401 Descr ...

  9. poj 3678 Katu Puzzle 2-SAT 建图入门

    Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...

随机推荐

  1. Spring 事务注解 错误问题

    碰到个问题,在一个springmvc项目中,设置好事务,然后在service上添加@Transactional注解,只要一添加这个注解,该service就无法被spring创建成功, error cr ...

  2. git服务器新增仓库

    在已有的git库中搭建新的库,并将本地的git仓库,上传到服务器的git库中,从而开始一个新的项目. 首先是在本地操作: 在本地新建文件夹spider,进入到spider中:如下

  3. Mybatis获取插入记录的自增长ID(转)

    1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <ins ...

  4. [CF442A] Borya and Hanabi (暴力bitmask)

    题目链接:http://codeforces.com/problemset/problem/442/A 题目大意:给你n张卡片,你知道这n张卡片都是什么,但是不知道他们的位置.你每次可以请求朋友指出一 ...

  5. PHP二维码生成的方法(google APi,PHP类库,libqrencode等)

    原文地址: http://blog.csdn.net/liuxinmingcode/article/details/7910975 ================================== ...

  6. ZAM 3D 制作3D动画字幕 用于Xaml导出

    原地址-> http://www.cnblogs.com/yk250/p/5662788.html 介绍:对经常使用Blend做动画的人来说,ZAM 3D 也很好上手,专业制作3D素材的XAML ...

  7. IaaS, PaaS, SaaS 解释

    IaaS.PaaS.SaaS作为云计算三种服务模式,下面会着重介绍他们的定义.作用.功能.对应产品以及他们之间的关系等. 一.定义:     SaaS:Software as a Service,软件 ...

  8. 所有古诗词的api

    服务商:api.getlove.cn 分类:诗词 更新时间:2016-03 如果接口不满足您的要求,您可以联系qq:2265658022购买完整的数据库 免费apikey:56eab527a0facb ...

  9. C#读写者线程(用AutoResetEvent实现同步)(转载)

    C#读写者线程(用AutoResetEvent实现同步) 1. AutoResetEvent简介 通知正在等待的线程已发生事件.无法继承此类. 常用方法简介: AutoResetEvent(bool ...

  10. javascript call与apply关键字的作用

    apply接受两个参数.第一个参数指定函数体内this对象的指向,第二个参数为一个带下标的集合. call则是apply的语法糖,如果参数数量固定,则可以不用带下标的集合传第二个参数. 1 2 3 4 ...