http://poj.org/problem?id=3678

总觉得这题比例题简单。

设a为x取0的点,a+n为x取1的点。

我们还是定义a到b表示取a必须取b。

那么我们有:

当AND:

1.当c=1:add(a,a+n); add(b,b+n);//我们不能取0的点,所以我们让程序一旦取0必会矛盾,下面类似的同理。

2.当c=0:add(a+n,b); add(b+n,a);

当OR

1.当c=1:add(a,b+n);add(b,a+n);

2.当c=0:add(a+n,a);add(b+n,b);

当OR

1.当c=1:add(a+n,b);add(b+n,a);add(a,b+n); add(b,a+n);

2.当c=0:add(a+n,b+n);add(b+n,a+n);add(a,b);add(b,a);

剩下的就是2-SAT(tarjan缩点)的活了。

#include<stack>
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
inline int read(){
int x=,w=;char ch=;
while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*w;
}
const int N=;
const int M=;
struct node{
int to;
int nxt;
}edge[M*];
int head[N],dfn[N],low[N],to[N];
int n,m,t,l,cnt;
bool instack[N*];
stack<int>q;
inline void add(int u,int v){
cnt++;
edge[cnt].to=v;
edge[cnt].nxt=head[u];
head[u]=cnt;
return;
}
void tarjan(int u){
t++;
dfn[u]=t;
low[u]=t;
q.push(u);
instack[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(instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
int v;
l++;
do{
v=q.top();
q.pop();
instack[v]=;
to[v]=l;
}while(v!=u);
}
return;
}
//a为x取0的点,a+n为x取1的点
int main(){
int n=read();
int m=read();
for(int i=;i<=m;i++){
int a=read();
int b=read();
int c=read();
char op[];
scanf("%s",op);
if(op[]=='A'){
if(c){
add(a,a+n);
add(b,b+n);
}else{
add(a+n,b);
add(b+n,a);
}
}
if(op[]=='O'){
if(c){
add(a,b+n);
add(b,a+n);
}else{
add(a+n,a);
add(b+n,b);
}
}
if(op[]=='X'){
if(c){
add(a+n,b);
add(b+n,a);
add(a,b+n);
add(b,a+n);
}else{
add(a+n,b+n);
add(b+n,a+n);
add(a,b);
add(b,a);
}
}
}
for(int i=;i<n*;i++){
if(!dfn[i])tarjan(i);
}
for(int i=;i<n;i++){
if(to[i]==to[i+n]){
printf("NO\n");
return ;
}
}
printf("YES\n");
return ;
}

POJ3678:Katu Puzzle——题解的更多相关文章

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

  2. poj3678 Katu Puzzle 2-SAT

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6714   Accepted: 2472 Descr ...

  3. POJ-3678 Katu Puzzle 2sat

    题目链接:http://poj.org/problem?id=3678 分别对and,or,xor推出相对应的逻辑关系: 逻辑关系 1 0  A and B     A'->A,B'->B ...

  4. POJ3678 Katu Puzzle

    原题链接 \(2-SAT\)模板题. 将\(AND,OR,XOR\)转换成\(2-SAT\)的命题形式连边,用\(tarjan\)求强连通分量并检验即可. #include<cstdio> ...

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

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

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

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

  8. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

  9. POJ 3678 Katu Puzzle (经典2-Sat)

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

随机推荐

  1. R语言使用过程中出现的问题--attach()函数的使用

    使用attach(file)时,一定要配合使用detach(file),否则再此运行程序时极易出现问题,The following objects are masked ... 此外工作空间中不能有与 ...

  2. JavaScript实现无刷新评论及在IE下的剪切板访问(学习)

    1.无刷新评论 tips: appendChild:将新元素作为父元素的最后一个子元素进行添加. insertBefore:在一个指定的子节点之前插入一个节点 实现: <!DOCTYPE htm ...

  3. HDU - 6409:没有兄弟的舞会(数学+思维)

    链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...

  4. Spring Cloud(六):Hystrix 监控数据聚合 Turbine【Finchley 版】

    Spring Cloud(六):Hystrix 监控数据聚合 Turbine[Finchley 版]  发表于 2018-04-17 |  更新于 2018-05-07 |  上一篇我们介绍了使用 H ...

  5. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  6. 安装HIVE

    参考:https://cwiki.apache.org/confluence/display/Hive/GettingStarted 1.下载hive安装包     到apache官网或者其它地方下载 ...

  7. POJ 3308 Paratroopers(最大流最小割の最小点权覆盖)

    Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the ...

  8. Java学习个人备忘录之抽象类

    抽象类 特点:1. 方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰,抽象方法必须定义在抽象类中.该类必须也被abstract修饰2. 抽象类不可以被实例化. 为什么?  因为调 ...

  9. 代替iframe的方法

    $('#framecont').html('').load("pageURL"); 使用jQuery.

  10. 华为oj----iNOC产品部-杨辉三角的变形 .

    此题提供三种方法,第一种,一开始就能想到的,设置一个足够大的数组存储生成的杨辉三角,然后进行判断就行,此方法参见:华为oj iNOC产品部-杨辉三角的变形 另一种方法是采用递归: 三角形的每行的个数为 ...