POJ 3678
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7391 | Accepted: 2717 |
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:
|
|
|
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
Source
#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的更多相关文章
- POJ 2837 Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45515 Accepted: 15434 Description Bes ...
- Backward Digit Sums(POJ 3187)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5495 Accepted: 31 ...
- POJ 2965&&1753
最近由于复习备考(然而考得还是很炸),很久没打题目了.现在开始刷寒假作业,不得不搞POJ 话说没有中文真的好烦啊! 先看1753 题目大意是说在一个4*4的格子中有黑白两色的棋子,你可以翻动其中的棋子 ...
- 【POJ】1830 开关问题(高斯消元)
http://poj.org/problem?id=1830 高斯消元无解的条件:当存在非法的左式=0而右式不等于0的情况,即为非法.这个可以在消元后,对没有使用过的方程验证是否右式不等于0(此时因为 ...
- dp优化
入口 A(fzu 1894) 普通的单调队列,trick是进队判断的符号选取(>=wa , >ac). B(poj 2823) 没什么好说的 ,坑爹poj g++,tle ;c++,ac. ...
- HF-01
胡凡 本书在第2章对C语言的语法进行了详细的入门讲解,并在其中融入了部分C+的特性. 第3-5章是 入门部分. 第3章 初步训练读者最基本的编写代码能力: 第4章对 常用介绍,内容重要: 第5章是 ...
- PencilWang博客目录
在这里有一坨目录,以后自己和别人看随笔都会方便很多 一 .刷题相关 1.BZOJ BZOJ1001(最大流,最短路)(EASY+) BZOJ1002(数学)(NORMAL+) BZOJ1003( ...
- 《Pro AngularJS》学习小结-01
<Pro AngularJS>该书以一个SportsStore案例为主线铺开. 一.开发环境设置 该书中所用的数据库data server开发环境是Deployed,从来没听说过,而且作者 ...
- POJ 1185 - 炮兵阵地 & HDU 4539 - 郑厂长系列故事——排兵布阵 - [状压DP]
印象中这道题好像我曾经肝过,但是没肝出来,现在肝出来了也挺开心的 题目链接:http://poj.org/problem?id=1185 Time Limit: 2000MS Memory Limit ...
随机推荐
- [笔记]--在Windows下配置Git
安装就不多说了: 1.ls不能显示中文目录 解决办法:在git/etc/git-completion.bash中增加一行: alias ls='ls --show-control-chars --co ...
- Secondary IP Addressing
Secondary IP Addressing secondary IP addressing. Secondary addressing uses multiple networks or subn ...
- C# WinForm自定义控件响应键盘事件
自己定义的winform控件,用其他键盘事件都无法响应,只有用ProcessCmdKey事件可以达到目的(别忘了主窗体的KeyPreview属性要设置为true),写法如下: prot ...
- hdu 5101 Select
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5101 Select Description One day, Dudu, the most cleve ...
- golang的goroutine与channel
Golang的goroutine是非抢占式的, 令人相当蛋疼! 有痛不能呻吟...只能配合channel在各goroutine之间传递信号来实现抢占式, 而这形成了golang最灵活与最具性能的核心. ...
- linux下操作
一.没有正确安装GNOME电源管理器的默认配置 二.oracle启停 1. linux下启动oraclesu - oraclesqlplus /nologconn /as sysdbastartupe ...
- scjp考试准备 - 7 - Java构造器
题目——如下代码的执行结果: class Hello{ String title; int value; public Hello(){ title += " World!"; } ...
- 如何在Window上使用Git
开始的时候同事只给了一个地址,类似这样:git@111.111.1.1:ABCDEF (1)如何在Windows上使用Git 有一篇博客不错:http://www.tuicool.com/articl ...
- C#泛型集合之Dictionary<k, v>使用技巧
1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...
- 霍夫变换(hough transform)
x-y轴坐标:y=kx+b k-b轴坐标:b=-xk+y θ-r轴坐标: