Katu Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7391   Accepted: 2717

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.

Source

 
2-sat 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack> using namespace std; const int MAX_N = ;
const int edge = 1e6 + ;
int first[ * MAX_N],Next[ * edge],v[ * edge];
int N,M,dfs_clock,scc_cnt;
int low[ * MAX_N],pre[ * MAX_N],cmp[ * MAX_N];
stack<int > S; void dfs(int u) {
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int e = first[u]; e != -; e = Next[e]) {
if(!pre[ v[e] ]) {
dfs(v[e]);
low[u] = min(low[u],low[ v[e] ]);
} else {
if(!cmp[ v[e] ]) {
low[u] = min(low[u],pre[ v[e] ]);
}
}
} if(pre[u] == low[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
} void scc() {
dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre)); for(int i = ; i < * N; ++i) if(!pre[i]) dfs(i);
} void add_edge(int id,int u) {
int e = first[u];
Next[id] = e;
first[u] = id;
} bool solve() {
scc();
for(int i = ; i < N; ++i) {
if(cmp[i] == cmp[N + i]) return false;
}
return true;
} void build(int a,int b,int c,char ch[],int &len) {
if(strcmp(ch,"AND") == ) {
if(c == ) {
v[len] = b;
add_edge(len++,a + N);
v[len] = a;
add_edge(len++,b + N);
} else {
v[len] = b + N;
add_edge(len++,a + N);
v[len] = a + N;
add_edge(len++,b + N);
v[len] = a + N;
add_edge(len++,a);
v[len] = b + N;
add_edge(len++,b);
}
}
if(strcmp(ch,"OR") == ) {
if(c == ) {
v[len] = b;
add_edge(len++,a);
v[len] = b;
add_edge(len++,b + N);
v[len] = a;
add_edge(len++,b);
v[len] = a;
add_edge(len++,a + N);
} else {
v[len] = b + N;
add_edge(len++,a);
v[len] = a + N;
add_edge(len++,b);
}
}
if(strcmp(ch,"XOR") == ) {
if(c == ) {
v[len] = b;
add_edge(len++,a);
v[len] = a + N;
add_edge(len++,b + N);
v[len] = a;
add_edge(len++,b);
v[len] = b + N;
add_edge(len++,a + N);
} else {
v[len] = b + N;
add_edge(len++,a);
v[len] = a + N;
add_edge(len++,b);
v[len] = b;
add_edge(len++,a + N);
v[len] = a;
add_edge(len++,b + N);
}
} } int main()
{
//freopen("sw.in","r",stdin);
scanf("%d%d",&N,&M);
for(int i = ; i < * N; ++i) first[i] = -;
int len = ;
for(int i = ; i <= M; ++i) {
int a,b,c;
char ch[];
scanf("%d%d%d%s",&a,&b,&c,ch);
build(a,b,c,ch,len); } printf("%s\n",solve() ? "YES" : "NO");
//cout << "Hello world!" << endl;
return ;
}

POJ 3678的更多相关文章

  1. POJ 2837 Til the Cows Come Home

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45515   Accepted: 15434 Description Bes ...

  2. Backward Digit Sums(POJ 3187)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5495   Accepted: 31 ...

  3. POJ 2965&&1753

    最近由于复习备考(然而考得还是很炸),很久没打题目了.现在开始刷寒假作业,不得不搞POJ 话说没有中文真的好烦啊! 先看1753 题目大意是说在一个4*4的格子中有黑白两色的棋子,你可以翻动其中的棋子 ...

  4. 【POJ】1830 开关问题(高斯消元)

    http://poj.org/problem?id=1830 高斯消元无解的条件:当存在非法的左式=0而右式不等于0的情况,即为非法.这个可以在消元后,对没有使用过的方程验证是否右式不等于0(此时因为 ...

  5. dp优化

    入口 A(fzu 1894) 普通的单调队列,trick是进队判断的符号选取(>=wa , >ac). B(poj 2823) 没什么好说的 ,坑爹poj g++,tle ;c++,ac. ...

  6. HF-01

    胡凡 本书在第2章对C语言的语法进行了详细的入门讲解,并在其中融入了部分C+的特性. 第3-5章是 入门部分. 第3章 初步训练读者最基本的编写代码能力: 第4章对 常用介绍,内容重要: 第5章是   ...

  7. PencilWang博客目录

    在这里有一坨目录,以后自己和别人看随笔都会方便很多 一 .刷题相关 1.BZOJ BZOJ1001(最大流,最短路)(EASY+)   BZOJ1002(数学)(NORMAL+)  BZOJ1003( ...

  8. 《Pro AngularJS》学习小结-01

    <Pro AngularJS>该书以一个SportsStore案例为主线铺开. 一.开发环境设置 该书中所用的数据库data server开发环境是Deployed,从来没听说过,而且作者 ...

  9. POJ 1185 - 炮兵阵地 & HDU 4539 - 郑厂长系列故事——排兵布阵 - [状压DP]

    印象中这道题好像我曾经肝过,但是没肝出来,现在肝出来了也挺开心的 题目链接:http://poj.org/problem?id=1185 Time Limit: 2000MS Memory Limit ...

随机推荐

  1. Java当中的异常

    异常:中断了正常指令流的事件,是JVM虚拟机产生的对象 异常是程序运行时产生的,和编译无关 class Test{ public static void main(String args[]){ Sy ...

  2. java中的接口回调

    [接口回调]接口回调是多态的另一种体现.接口回调是指:可以把使用某一个接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口中的方法.当接口变量调用被类实现的接口中 ...

  3. zip解压缩

    package com.green.project.compress; import java.io.File;import java.io.FileInputStream;import java.i ...

  4. poj 1985 Cow Marathon

    题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...

  5. RCF

    1. RCF: 纯c++的RPC, 不引入IDL, 大量用到boost,比较强大.2. casocklib:  protobuf + asio 较完善实现3. eventrpc: protobuf + ...

  6. Go defer延迟执行

    defer用于延迟执行,可以类比于java或c++中的析构函数. 查看一段示例代码: func Contents(filename string) (string, error) { //打开文件 f ...

  7. php连接mysql报错No such file or directory

    php测试文件如下: 1 2 3 4 5 6 7 8 9 10 11 <?php $con = mysql_connect("localhost","root&qu ...

  8. MVC 中的@Html.DropDownList下拉框的使用

    MVC 中的下拉框 实现方式,下面为大家介绍一个我自己认为比较好用的实现方式,而二话不说直接上代码: 第一步: 后台代码 //公共的方法 //在每次需要展示下拉框的时候,需要调用一下这个方法 [数据源 ...

  9. SQL中一种类似GUID值的函数实现

        开发中会需要用到多列值组合成一个ID值的情况.比如做数据清洗的时候,一张表A有五列,分别是医院.科室.医生.职称.电话.面有许多重复的数据需要和另一个表B(和A列相同)做对比.清洗需要做两件事 ...

  10. EntityFramwork(1) 源地址https://msdn.microsoft.com/zh-cn/data/jj193542

    1.创建应用程序 简单起见,我们将构建一个使用 Code First 执行数据访问的基本控制台应用程序. 打开 Visual Studio "文件"->"新建&qu ...