poj3678 Katu Puzzle 2-SAT
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:
|
|
|
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>
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的更多相关文章
- 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 ...
- POJ-3678 Katu Puzzle 2sat
题目链接:http://poj.org/problem?id=3678 分别对and,or,xor推出相对应的逻辑关系: 逻辑关系 1 0 A and B A'->A,B'->B ...
- poj 3678 Katu Puzzle(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
- POJ3678 Katu Puzzle
原题链接 \(2-SAT\)模板题. 将\(AND,OR,XOR\)转换成\(2-SAT\)的命题形式连边,用\(tarjan\)求强连通分量并检验即可. #include<cstdio> ...
- 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 ...
- 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 ...
- POJ 3678 Katu Puzzle (2-SAT)
Katu Puzzle Time Limit: 1000MS ...
- POJ 3678 Katu Puzzle (经典2-Sat)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 2401 Descr ...
- 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 ...
随机推荐
- Vmware workstation 11 安装 RedHat 9 时 第二个iso文件 出现光盘无法被挂载
通过虚拟机装linux系统,RedHat 9有3个iso文件,安装第一个iso文件时很顺利,安装完成第一个iso文件后,提示请插入光盘 需要继续安装第二个和第三个iso文件,点击菜单栏——虚拟机——设 ...
- SparkConf加载与SparkContext创建(源码阅读四)
sparkContext创建还没完呢,紧接着前两天,我们继续探索..作死... 紧接着前几天我们继续SparkContext的创建: 接下来从这里我们可以看到,spark开始加载hadoop的配置信息 ...
- 如何用Jupyter Notebook打开Spark
电脑已经装了anaconda python,然后下载了spark2.1.0.因为版本太新,所以网上和书上的一些内容已经不再适用.比如关于如何使用IPython和Jupyter,教程给出的方法是用如下语 ...
- [AS3.0] Error #2044: 未处理的 NetStatusEvent:。 level=error, code=NetStream.Record.NoAcces 解决办法
突然又需要FMS做视频录制,却遇到一些意想不到的问题,我在想当年做的时候怎么没遇到... 报错: Error #2044: 未处理的 NetStatusEvent:. level=error, cod ...
- C++模板机制总结
模板是C++中非常重要的组成部分,之前自己对这块领域一直不太熟悉.最近趁着有时间学习了一下,特此总结. 首先是函数模板,它的定义方式如例子所示: template <typename T> ...
- Python mysql 操作小类,供大家用用
import binascii import os import linecache import time #add pyDes path #sys.path.append("/data1 ...
- Python基础篇【第7篇】: 面向对象(2)
上一篇<初识面向对象>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公 ...
- Linux学习笔记之——基础命令学习
1.find 按照名字查找:find / -name file_name 2.zip压缩 1) 我想把一个文件repartition.txt和一个目录invader压缩成为amateur.zip: ...
- URL中文转码问题
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Java:国际化
Java的国际化: 资源文件的命名可以有如下三种形式:baseName _ language _country.properties baseName _language.properties bas ...