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. Android开发遇到的异常及解决办法

    Android开发遇到的错误及解决方法1. Unable to resolve target 'android-7' 解决方案: 修改工程目录下的default.properties文件里的内容tar ...

  2. 别跟我来这套 Hot Swap 热插拔

    如果你觉得我们演的不好,可以随时打断,如果你觉得怎么演好可以随时来改,你都可以直接来演.

  3. SQL 执行顺序

    SQL 是一种声明式语言,与其他语言相比它的最大特点是执行顺序-并非按照语法顺序来执行.因此很多程序猿看到SQL就头疼,我之前也是这样,后来看到一篇文章后豁然开朗-地址. 理解了SQL的执行顺序无疑对 ...

  4. 【PHP】iOS推送通知以及反馈服务

    近来项目是完成一个PHP的推送服务器,无论是PHP,APNs还是GCM基本上都是从零开始. 写下一点见解,方便以后继续做代码的搬运工. 因为对PHP跟iOS都不熟悉,可能有错漏...穷孩子没有用过iO ...

  5. Notes of the scrum meeting(11/3)

    meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪                  ...

  6. 电梯调度--c++--软件工程

    一.设计思路 (1)将乘客要去的楼层数存起来. (2)假设yi为乘客要爬楼层数之和,yi=n1*|(n1-ni)|+n2*|(n2-ni)|+..+n18*|(n18-ni)| (3)比较y1到y18 ...

  7. Python字符编码详解

    1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码.计算机世界里一开始只有 ...

  8. android中的“visible ”、“invisible”、“gone”的区别(转载)

    在Android开 发中,大部分控件都有visibility这个属性,其属性有3个分别为“visible ”.“invisible”.“gone”.主要用来设置控制控件的显示和隐藏.有些人可能会疑惑I ...

  9. maven插件:tomcat插件和jetty插件的区别

    在程序是多模块结构的时候,使用tomcat的maven插件和jetty的maven插件有细微差别: 1.tomcat7-maven-plugin   可以直接在parent的邮件直接运行:tomcat ...

  10. LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...