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 ...
随机推荐
- OAF_开发系列08_实现OAF通过Popup参数式弹出窗口(案例)
20150711 Created By BaoXinjian
- css3 flex盒子布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- weblogic端口号修改和内存参数配置
1 端口号修改 如图 是详细路径 注:我用的weblogic版本是10.3 当刚创建完域的时候这个配置文件下没有Listen-port参数 第一次去控制台修改端口后就这个参数了
- SOAP(简单对象访问协议)
ylbtech-Miscellaneos:SOAP(简单对象访问协议) A,返回顶部 1, 简单对象访问协议是交换数据的一种协议规范,是一种轻量的.简单的.基于XML(标准通用标记语言下的一个子集)的 ...
- 通过单元测试理解spring容器以及dubbo+zookeeper单元测试异常处理
一.先说一个结论:单元测试与主项目的spring容器是隔离的,也就是说,单元测试无法访问主项目spring容器,需要自己加载spring容器. 接下来是代码实例,WEB主项目出于运行状态,单元测试中可 ...
- 用python脚本通过excel生成文件夹树结构
大概这样写标题是对的吧... 目标: 通过excel目录结构文档生成文件夹树结构. 也就是: 通过下面的excel
- oracle补丁升级
PSU的全称是Patch Set Update,Oracle对于其产品每个季度发行一次的补丁包,包含了bug的修复.Oracle选取被用户下载数量多,且被验证过具有较低风险的补丁放入到每个季度的PSU ...
- No Architectures to Compile for (ONLY_ACTIVE_ARCH=
No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=armv7, VA 运行报错 出现的原因:armv7s是应用在iP ...
- c#摄像头编程实例 (转)
c#摄像头编程实例 摄像头编程 安装摄像头后,一般可以找到一个avicap32.dll文件 这是一个关于设想头的类 using system;using System.Runtime.Intero ...
- LPC2478_调试心得(转)
1.在调试“E:\htwang\smart2200v201\ARM嵌入式系统实验教程(二)\开发板出厂编程程序\液晶显示程序\LCM_Disp”的程序时,想使用外部RAM进行仿真调试,在将ADS1.2 ...