Description

Katu Puzzle is presented as a directed graph G(VE) 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 ≤ X≤ 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 (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.

   根据上面直接建图
 
  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack> using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 0x7ffffff;
struct node {
int v, next;
} edge[maxn];
int head[maxn], dfn[maxn], low[maxn];
int s[maxn], belong[maxn], instack[maxn];
int tot, cnt, top, flag, n, m;
void init() {
tot = cnt = top = flag = ;
memset(s, , sizeof(s));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(instack, , sizeof(instack));
}
void add(int u, int v ) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjan(int v) {
dfn[v] = low[v] = ++flag;
instack[v] = ;
s[top++] = v;
for (int i = head[v] ; ~i ; i = edge[i].next ) {
int j = edge[i].v;
if (!dfn[j]) {
tarjan(j);
low[v] = min(low[v], low[j]);
} else if (instack[j]) low[v] = min(low[v], dfn[j]);
}
if (dfn[v] == low[v]) {
cnt++;
int t;
do {
t = s[--top];
instack[t] = ;
belong[t] = cnt;
} while(t != v) ;
}
}
int check() {
for (int i = ; i < n ; i++)
if (belong[ * i] == belong[ * i + ]) return ;
return ;
}
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if (n == && m == ) break;
init();
char op[];
int x, y, c;
for (int i = ; i < m ; i++) {
scanf("%d%d%d%s", &x, &y, &c, op);
if (op[] == 'A') {
if (c) {
add( * x + , * x);
add( * y + , * y);
} else {
add( * x, * y + );
add( * y, * x + );
}
}
if (op[] == 'O') {
if (c) {
add( * x + , * y);
add( * y + , * x);
} else {
add( * x, * x + );
add( * y, * y + );
}
}
if (op[] == 'X') {
if (c) {
add( * x, * y + );
add( * x + , * y);
add( * y, * x + );
add( * y + , * x);
} else {
add( * x + , * y + );
add( * x, * y);
add( * y + , * x + );
add( * y, * x);
}
} }
for (int i = ; i < * n ; i++)
if (!dfn[i]) tarjan(i);
if (check()) printf("YES\n");
else printf("NO\n");
}
return ;
}

poj 3678 Katu Puzzle 2-SAT 建图入门的更多相关文章

  1. poj 3678 Katu Puzzle(Two Sat)

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

  2. [poj 3678]Katu Pazzle[2-SAT常用建图法]

    题意: 不说了..典型的2-SAT 常用模型: 重点: 突出"绑定性". 连线表示限制而非可行. 因为最后要求对立点不在同一强连通分量是说同一强连通中的点必须同时选. 坑: 首先是 ...

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

  5. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

  6. POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9987   Accepted: 3741 Descr ...

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

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

  8. POJ 3678 Katu Puzzle(强连通 法)

    题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...

  9. POJ 3678 Katu Puzzle (2-SAT,常规)

    题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) ...

随机推荐

  1. Servlet学习笔记03——什么是DAO?

    1.案例 (1)添加用户 step1.建表 create table t_user( id int primary key auto_increment, username varchar(50) u ...

  2. 《Linux就该这么学》,刘小伙实在人,给打个广告

    本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材,目前是国内最值得去读的Linux教材,也是最有价值 ...

  3. 简单了解,使用oracle中的索引,表分区

    索引的分类 如下: 物理分类 逻辑分类 分区或非分区索引 单列或组合索引 B树索引(标准索引) 唯一或非唯一索引 正常或反向键索引 基于函数索引 位图索引   B树索引 b树索引通常也称为标准索引,索 ...

  4. 【shopex】添加网页挂件widgets

    一步步做个widgets 基础篇 首先建立一个目录plugins/widgets/helloword 这个就是新的挂件目录,系统的每个widgets都是一个目录 里面放两个文件: widgets.ph ...

  5. asp.net core-项目开发中问题汇总

    无法启动进程\Program File\dotnet\dotnet.exe.进程创建失败,出现错误:系统找不到指定的文件如下图: 解放方案:1.修改系统环境变量 2.重启电脑

  6. linux上面安装svn步骤

    一.安装 使用yum,非常简单 yum install subversion 二.配置 2.1.创建仓库 我们这里在/home下建立一个名为svn的仓库(repository),以后所有代码都放在这个 ...

  7. yii2深入理解之内核解析

    一.前言 首先,yii2最为为数不多的PHP主流开源框架,受欢迎程度不亚于laravel和TP.个人认为,研究这些框架底层代码是非常有助于自身代码编程思想的提升和代码简化程度和质量的提升的. 那么,话 ...

  8. 爬虫之urllib模块

    1. urllib模块介绍 python自带的一个基于爬虫的模块. 作用:可以使用代码模拟浏览器发起请求. 经常使用到的子模块:request,parse. 使用流程: 指定URL. 针对指定的URL ...

  9. LAMP架构的搭建

    什么是LAMP架构? L : Linux,2.6.18-308.el5(redhat5.8) A :Apache,httpd 2.4.4 M :  mysql-5.5.28  P : php-5.4. ...

  10. Skyscrapers Covered in Solar Pancels【太阳能电池板覆盖的摩天大楼】

    Skyscrapers Covered in Solar Panels An office tower on Miller Stree in Manchester is completely cove ...