Katu Puzzle
poj3678:http://poj.org/problem?id=3678
题意:给你一些数,然后这些要么是0要么是1,然后回给出一些数之间的and,or,xor的值,问你是否存在一组解。
题解:2-sat的一道很好的题目。能很好训练建边的思想。建边如下。
and==1: 说明 a,b必须选,就是必须都是1,所以a->~a,b->~b;
ans==0,说明 a,b不能同时都选,那么选择了~a就只能选择b,选择了~b就只能选择a;所以有边,~a-->b,~b->a;
or==1说明至少有一个是1 ,至少取一个,a-->~b;b-->~a;
or==0说明都不取, ~a->a;~b->b;
xor==1建边 x->~y,y->~x,~y->x,~x->y (两个数必须不同)
xor==0 建边 x->y,y->x,~x->~y,~y->~x (两个数必须相同)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+;
const int M=1e7+;
struct Edge{
int to,next;
} edge[M];
int n,m,cnt,dep,top,atype;
int dfn[N],low[N],vis[N],head[N],st[N],belong[N],in[N],out[N],sum[N];
//sum[i]记录第i个连通图的点的个数,in[i],out[i],表示缩点之后点的入度和初度。
void init(){
cnt=dep=top=atype=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(belong,,sizeof(belong));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(sum,,sizeof(sum));
}
void addedge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void Tarjan(int u){
dfn[u]=low[u]=++dep;
st[top++]=u;
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
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=st[--top];
belong[j]=atype;
sum[atype]++; //记录每个连通分量中点的个数
vis[j]=;
}
while(u!=j);
}
}
char str[];
int u,v,w;
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i=;i<=m;i++){
scanf("%d%d%d%s",&u,&v,&w,str);
u++;v++;
if(str[]=='A'){
if(w==){
addedge(u,u+n);
addedge(v,v+n);
}
else{
addedge(u+n,v);
addedge(v+n,u);
}
}
else if(str[]=='O'){
if(w==){
addedge(u,v+n);
addedge(v,u+n);
}
else {
addedge(u+n,u);
addedge(v+n,v);
}
}
else if(str[]=='X'){
if(w==){
addedge(u,v+n);
addedge(u+n,v);
addedge(v+n,u);
addedge(v,u+n);
}
else{
addedge(u,v);
addedge(v,u);
addedge(u+n,v+n);
addedge(v+n,u+n);
}
}
}
for(int i=;i<=*n;i++)
if(!dfn[i])Tarjan(i);
bool flag=false;
for(int i=;i<=n;i++){
if(belong[i]==belong[i+n]){
flag=true;
break;
}
}
if(flag)printf("NO\n");
else
printf("YES\n");
}
}
Katu Puzzle的更多相关文章
- poj3678 Katu Puzzle 2-SAT
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6714 Accepted: 2472 Descr ...
- 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) - 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)
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 ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- 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
Description 给出一个关系,包括 And,Xor,Or 问是否存在解. Sol 经典的2-SAT问题. 把每个值看成两个点,一个点代表选 \(0\) ,另一个代表选 \(1\) . 首先来看 ...
- POJ 3678 Katu Puzzle(强连通 法)
题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...
随机推荐
- pugixml
http://www.firedragonpzy.com.cn/index.php/archives/3227 有关cocos2d-x的xml文件读取问题
- CSS中Position属性
也许你看到这个标题觉得很简单,确实这是一篇关于CSS中Position属性基础知识的文章,但是关于Position的一些细节也许你不了解. 1.简介 position有五个属性: static | r ...
- HTML+CSS 整站 步骤
文件夹管理: CSS JS img font html 根据设计图,划分区块 ,即页面布局 重置样式 ;padding:0;} 写main.css 注意:1 距离尽量使用偶数,避免奇数 2 在使用定 ...
- php快速排序
快速排序是排序中常用的,效率据说还不错,它使用分治算法实现 将一个大的需要排序的序列,分成两个较小的序列!怎么分呢,需要从序列中找出一个元素作为参考元素,通常的做法是拿第一个元素作为参考元素.当一个序 ...
- Animating Layout Changes(展开收起)
原文地址:https://developer.android.com/training/animation/layout.html#add (1)设置布局文件: <LinearLayout an ...
- 使用AsyncHttpClient碰到的问题及解决方法
之前做一个项目,项目里面的布局是这样的:一个Viewpager,Viewpager里面有三个Fragment,在第二个Fragment里面有一个ListView,使用了BaseAdapter来显示it ...
- window.clearInterval与window.setInterval的用法(
window.setInterval() 功能:按照指定的周期(以毫秒计)来调用函数或计算表达式. 语法:setInterval(code,millisec) 解释:code:在定时时间到时要执行的J ...
- EditPlus 快捷键
FileFtpUpload Ctrl+Shift+S 上传文件到 FTP 服务器 FileNew Ctrl+N 新建普通的文本文档 Fi ...
- /etc目录深入理解
/etc This is the nerve center of your system, it contains all system related configuration files in ...
- 【转】Objective-C中的instancetype和id关键字
原文:http://blog.csdn.net/wzzvictory/article/details/16994913 一.什么是instancetype instancetype是clang 3.5 ...