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 ...
随机推荐
- 使用的 SQL Server 版本不支持数据类型“datetime2”.
错误原因,在使用ado.net entity的时候,entity使用的数据库是sqlserver 2008, 但后来实际使用中使用的数据库是sqlserver 2005, 使用的 SQL Server ...
- Linux 开机启动
Linux开机启动(bootstrap) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 计算机开机是一个神秘的过程.我们只是 ...
- 网络编程(一):用C#下载网络文件的2种方法
使用C#下载一个Internet上的文件主要是依靠HttpWebRequest/HttpWebResonse和WebClient.具体处理起来还有同步和异步两种方式,所以我们其实有四种组合. 1.使用 ...
- 说说无耻的商河水木清华开发商2013"交房
说说无耻的水木清华开发商2013"交房" 我买的是22号楼,合同里写的是2011年6月30号前交房.4月28我手机响了,电话那边说是水木清华的,29号交房.说交房通知书已经EMS发 ...
- linux svn 用户配置
1:创建存放仓库的目录 mkdir -p /home/svn/ 2:创建svn仓库 svnadmin create /home/svn/evansource 3:配置仓库(一共三个文件夹auzhz.p ...
- Good Practices to Write Stored Procedures in SQL Server
Reference to: http://www.c-sharpcorner.com/UploadFile/skumaar_mca/good-practices-to-write-the-stored ...
- HandlerThread和IntentService
HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...
- 如何在IDEA上创建Spring MVC项目
对于刚刚从eclipse.myeclipse转到IDEA工具,在搭建项目遇到了一些问题,所以让我来分享我的搭建过程. 建议大家准备java环境.IDEA工具.tomcat.maven了,还有我是win ...
- C#中用RichTextBox实现图文混排和保存的例子
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; us ...
- Spring使用非applicationContext.xm 默认名的配置文件的配置
Spring默认的配置文件是applicationContext.xml,但是有些时候,希望拆分Spring的配置文件,让其单一化,每一个都只进行自己的配置,如图所示 那么就需要在web.xml中配置 ...