Katu Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6553   Accepted: 2401

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问题

构图时,根据条件找可以确定关系的形如A->B这样的关系式

i表示i取1,~i表示i取0

i AND j =1    ~i->i, ~j->j, i->j, j->i,后面两个关系式构成一个环,i,j在同一强连通分量中,可以免去

i AND j = 0   i->~i, j->~j 而~i推不出j为0还是1

i OR   j =1    ~i->j, ~j->i

i OR   J =0    i->~i,  j->~j, ~j->~i, ~i->~j 又有环,可以省略

i XOR j =1    i->~j,  j->~i,  ~i->j,  ~j->i

i XOR j =0    i->j,  j->i,  ~i->~j, ~j->~i   又构成两个环

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
}edge[EM<<]; int n,m,cnt,dep,top,atype,head[VM];
int dfn[VM],low[VM],vis[VM],belong[VM];
int stack[VM]; void Init(){
cnt=, atype=, dep=, top=;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(belong,,sizeof(belong));
} void addedge(int cu,int cv){
edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++;
} void Tarjan(int u){
dfn[u]=low[u]=++dep;
stack[top++]=u;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}else if(vis[v])
low[u]=min(low[u],dfn[v]);
}
int j;
if(dfn[u]==low[u]){
atype++;
do{
j=stack[--top];
belong[j]=atype;
vis[j]=;
}while(u!=j);
}
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
Init();
char op[];
int i,j,c;
while(m--){
scanf("%d%d%d%s",&i,&j,&c,op);
if(op[]=='A'){
if(c){
addedge(*i+,*i);
addedge(*j+,*j);
//addedge(2*i,2*j);//2*i和2*j在同一个环中,肯定满足
//addedge(2*j,2*i);
}else{
addedge(*i,*j+);
addedge(*j,*i+);
}
}else if(op[]=='O'){
if(c){
addedge(*i+,*j);
addedge(*j+,*i);
}else{
addedge(*i,*i+);
addedge(*j,*j+);
//addedge(2*i+1,2*j+1);//同上
//addedge(2*j+1,2*i+1);
}
}else{
if(c){
addedge(*i,*j+);
addedge(*i+,*j);
addedge(*j,*i+);
addedge(*j+,*i);
}else{
//addedge(2*i,2*j);
//addedge(2*j,2*i);
//addedge(2*i+1,2*j+1);
//addedge(2*j+1,2*i+1);
}
}
}
for(i=;i<*n;i++)
if(!dfn[i])
Tarjan(i);
int flag=;
for(i=;i<n;i++)
if(belong[*i]==belong[*i+]){
flag=;
break;
}
if(flag)
puts("YES");
else
puts("NO");
}
return ;
}

POJ 3678 Katu Puzzle (经典2-Sat)的更多相关文章

  1. 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 ...

  2. POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9987   Accepted: 3741 Descr ...

  3. 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 ...

  4. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

  5. 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 ...

  6. poj 3678 Katu Puzzle(Two Sat)

    题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...

  7. POJ 3678 Katu Puzzle 2-SAT 强连通分量 tarjan

    http://poj.org/problem?id=3678 给m条连接两个点的边,每条边有一个权值0或1,有一个运算方式and.or或xor,要求和这条边相连的两个点经过边上的运算后的结果是边的权值 ...

  8. POJ 3678 Katu Puzzle

    Description 给出一个关系,包括 And,Xor,Or 问是否存在解. Sol 经典的2-SAT问题. 把每个值看成两个点,一个点代表选 \(0\) ,另一个代表选 \(1\) . 首先来看 ...

  9. POJ 3678 Katu Puzzle(强连通 法)

    题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...

随机推荐

  1. Apache Mahout:适合所有人的可扩展机器学习框架

    http://www.ibm.com/developerworks/cn/java/j-mahout-scaling/ 在软件的世界中,两年就像是无比漫长的时光.在过去两年中,我们看到了社交媒体的风生 ...

  2. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十六)Structured Streaming:WARN clients.NetworkClient: Error while fetching metadata with correlation id 1 : {my-topic=LEADER_NOT_AVAILABLE}

    问题描述: 我之前使用kafka的命令删除了改topic: ./kafka-topics.sh --delete --zookeeper [zookeeper server]  --topic [to ...

  3. Provider Pattern for Beginners in .net

    Download ProviderPattern.zip Introduction Provider pattern allows the developers to create pluggable ...

  4. ASP入门(十三)-Server对象

    Server 对象用于处理服务器上的一些特殊任务,例如,创建组件实例.获取文件路径.执行ASP脚本文件等. Server 对象是体现 ASP 强大功能的一个对象,之前介绍的对象都是针对获取.请求以及简 ...

  5. 【Python】使用torrentParser1.03对单文件torrent的分析结果

    C:\Users\horn1\Desktop\python\42-torrentParser>python torrentParser.py 文件名=./5.torrent 文件结构: anno ...

  6. 【nodejs】理想论坛帖子下载爬虫1.08

    //====================================================== // 理想论坛帖子下载爬虫1.09 // 使用断点续传模式,因为网络传输会因各种原因中 ...

  7. VMware Lab setup - A virtualized lab for testing HA and DRS

    https://www.simple-talk.com/sysadmin/virtualization/vmware-lab-setup---a-virtualized-lab-for-testing ...

  8. 去掉JAVA部分依赖的事例

    一.现象 最近做JAVA项目,需要依赖一个外部的JAR包,但是依赖之后,发现eclipse一直workspace,估计是包重复加载的问题 二.问题 使用查看包依赖的命令:mvn dependency: ...

  9. Nutch的安装和配置

    Nutch是一个Java实现的网络爬虫.Nutch的安装可以使用二进制包,也可以使用源代码安装.这里介绍用二进制包安装. 1. 下载apache-nutch-1.12-bin.tar.gz,并且解压, ...

  10. Maven仓库下载jar包失败的处理方案

    Maven仓库下载jar包失败的处理方案 在使用Maven项目的时候,有时候中央仓库并没有对应的包比如kaptcha-2.3.2.jar: 为了使我们的 项目能够正常运行下去,我们可以去别的地方下载对 ...