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. elasticsearch增删改查操作

    目录 1. 插入数据 2. 更改数据 3. 删除数据 4. 检索文档 1. 插入数据 关于下面的代码如何使用,可以借助于kibana的console,浏览器打开地址: http://xxx.xxx.x ...

  2. ThinkPHP开启设置子域名笔记

    一.ThinkPHP框架里 common下的config文件 'APP_SUB_DOMAIN_DEPLOY' => 1, // 开启子域名配置 'APP_SUB_DOMAIN_RULES' =& ...

  3. 游戏人工智能 读书笔记 (四) AI算法简介——Ad-Hoc 行为编程

    本文内容包含以下章节: Chapter 2 AI Methods Chapter 2.1 General Notes 本书英文版: Artificial Intelligence and Games ...

  4. 如何理解一台服务器可以绑定多个ip,一个ip可以绑定多个域名

    一个域名只能对应一个IP的意思是域名在DNS服务器里做解析的时候 一条记录只能指向一个IP地址.这个是死规定,试想一下,如果一个子域名指向了2个ip ,当访问者打开这个域名的时候,浏览器是展示哪个IP ...

  5. 第3章 TCP协议详解

    第3章 TCP协议详解 3.1 TCP服务的特点 传输协议主要有两个:TCP协议和UDP协议,TCP协议相对于UDP协议的特点是 面向连接使用TCP协议通信的双方必须先建立连接,完成数据交换后,通信双 ...

  6. (C#)工厂方法模式

    1.工厂方法模式 第一了一个用于创建对象的接口,让子类自己决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. *工厂方法模式即克服了简单工厂模式违反开放-封闭原则的缺点,又保留了封装对象创建过 ...

  7. python中为什么 if/while/def/class语句需要冒号?

    python中冒号主要用于增强可读性(ABC语言实验的结果之一).考虑一下这个: if a == b print(a) 与 if a == b: print(a) 注意第二种方法稍微容易一些.请进一步 ...

  8. 名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)

    1. 题目描述 Problem Description Leetcode 277. Find the Celebrity Suppose you are at a party with n peopl ...

  9. SpringCloud IDEA 教学 (五) 断路器控制台(HystrixDashboard)

    写在开头 断路器控制台是为了查看断路器运行情况而研发的.本章介绍了断路器控制台的搭建,代码基于之前Client的搭建.HystrixDashboard基于之前配置好的,使用了HystrixComman ...

  10. Sublime Text 插件推荐——for web developers

    楼主向高大上的: web front-end development engineer (好吧,google就是这样翻译的 ^_^)们推荐 ST 插件,在此抛砖引玉: NO.1 :Emmet (原名: ...