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 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
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstdio>
#include<queue>
#define mem(a , b) memset(a , b , sizeof(a))
using namespace std ;
const int MAXN = 10000 ;
vector<int> G[MAXN * 2] ;
bool mark[MAXN * 2] ;
int S[MAXN] , c ; // 模拟栈
char op[8] ;
int n , m ;
int pan ; // 判断标志
void chu()
{
int i ;
for(i = 0 ; i < n * 2 ; i ++)
G[i].clear() ;
mem(mark , 0) ;
}
void init()
{
chu() ;
pan = 0 ;
int i ;
for(i = 0 ; i < m ; i ++)
{
int a , b , c ;
scanf("%d%d%d" , &a , &b , &c) ;
scanf("%s" , op) ;
if(op[0] == 'A')
{
if(c == 1) // 注意此时建边的方式
{
G[2 * a].push_back(2 * a + 1) ;
G[2 * b].push_back(2 * b + 1) ; }
else
{
G[2 * a + 1].push_back(2 * b) ;
G[2 * b + 1].push_back(2 * a) ;
}
}
else if(op[0] == 'X')
{
if(c == 0)
{
G[2 * a].push_back(2 * b) ;
G[2 * a + 1].push_back(2 * b + 1) ;
G[2 * b].push_back(2 * a) ;
G[2 * b + 1].push_back(2 * a + 1) ;
}
else
{
G[2 * a].push_back(2 * b + 1) ;
G[2 * a + 1].push_back(2 * b) ;
G[2 * b].push_back(2 * a + 1) ;
G[2 * b + 1].push_back(2 * a) ;
}
}
else
{
if(c == 0) // 注意此时建边的方式
{
G[2 * a + 1].push_back(2 * a) ;
G[2 * b + 1].push_back(2 * b) ;
}
else
{
G[2 * a].push_back(2 * b + 1) ;
G[2 * b].push_back(2 * a + 1) ;
}
}
}
}
bool dfs(int x)
{
if(mark[x ^ 1]) return false ;
if(mark[x]) return true ;
mark[x] = true ;
S[c ++] = x ;
int i ;
for(i = 0 ; i < G[x].size() ; i ++)
{
if(!dfs(G[x][i]))
return false ;
}
return true ;
}
void solve()
{
if(pan)
puts("NO") ;
else
{
int i ;
for(i = 0 ; i < n ; i ++)
{
if(!mark[i * 2] && !mark[i * 2 + 1])
{
c = 0 ;
if(!dfs(i * 2))
{
while (c > 0)
{
mark[ S[-- c] ] = false ;
}
if(!dfs(i * 2 + 1))
{
pan = 1 ;
break ;
}
}
}
}
if(pan)
puts("NO") ;
else
puts("YES") ;
}
}
int main()
{
while (scanf("%d%d" , &n , &m) != EOF)
{
init() ;
solve() ;
}
return 0 ;
}
POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang的更多相关文章
- POJ 3678 Katu Puzzle (经典2-Sat)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 2401 Descr ...
- POJ 3678 Katu Puzzle(强连通 法)
题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- POJ 3678 Katu Puzzle (2-SAT)
Katu Puzzle Time Limit: 1000MS ...
- POJ 3678 Katu Puzzle (2-SAT,常规)
题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) ...
- 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 建图入门
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
- POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- poj 3678 Katu Puzzle(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
随机推荐
- openstack API debug OpenstackEveryProject_CLI,curl_based
1,基于Openstack 每个服务组件client客户端,eg,nova 客户端软件包名称是python-novaclient, 别的都一样,把python-novaclient (nova替换成组 ...
- [C# 基础知识系列]专题九: 深入理解泛型可变性
引言: 在C# 2.0中泛型并不支持可变性的(可变性指的就是协变性和逆变性),我们知道在面向对象的继承中就具有可变性,当方法声明返回类型为Stream,我们可以在实现中返回一个FileStream的类 ...
- Unity 调用android插件
1. Unity的Bundle Identifier必须和你的android报名一致 Activity和View的区别: Activity应该是一个展示页面,View是页面上一些按钮视图等等. 如何调 ...
- vlc-android对于通过Live555接收到音视频数据包后的处理分析
通过ndk-gdb跟踪调试vlc-android来分析从连接到RTSP服务器并接收到音视频数据包后的处理过程. 首先,从前面的文章有分析过vlc-android的处理过程通过线程函数Run()(Src ...
- android 补间动画
android开发过程中,为了更好的展示应用程序,应用程序添加动画,能够很好地实现这个功能.如果动画中的图像变化有一定的规律,可以采用自动生成图像的方式来生成动画,例如图像的移动.旋转.缩放等.自动生 ...
- jq操作cookie
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ios 文字图标
如何使用自定义字体 在讲icon font之前,首先先来看看普通自定义字体是如何在ios中使用的,两个原理是一样的.这里以KaushanScript-Regular为例: Step 1: 导入字体文件 ...
- 关于textField
如果想给textField设置背景图片,首先设置该控件的bounder Style为最左边的无style,然后设置背景图片 如果设置textField弹出键盘 的发送按钮:设置右侧Return K ...
- 正确合理的建立MYSQL数据库索引
写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将对整 ...
- js 跨域的使用
try{document.domain="jincin.com"}catch(error){} 需要在被调用的函数和调用函数出都要加入上面相同的语句 下面看一下第二种跨域的解决方案 ...