Description

给出一个关系,包括 And,Xor,Or 问是否存在解.

Sol

经典的2-SAT问题.

把每个值看成两个点,一个点代表选 \(0\) ,另一个代表选 \(1\) .

首先来看 Xor :

如果两个值异或起来为 \(1\) :那么连边 \((i_0,j_1),(i_1,j_0),(j_0,i_1),(j_1,i_0)\) .

否则 连边 \((i_0,j_0),(i_1,j_1),(j_0,i_0),(j_1,i_1)\) .

然后是 And.

如果两个值 And 起来为 \(1\) :连边 \((i_0,i_1),(j_0,j_1)\)

否则 连边 \((i_1,j_0),(j_1,i_0)\)

最后是 Or.

如果两个值 Or 起来为 \(1\) :连边 \((i_0,j_1),(j_1,i_0)\)

否则 连边 \((i_1,i_0),(j_1,j_0)\) .

Tarjan 缩一下环判断一下两个点是否在一个环中就可以了.

Code

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std; const int N = 2005; int n,m,cnt,cntb;
vector< int > g[N];
int d[N],b[N],ins[N];
int stk[N],top; inline int in(int x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();return x; }
void Add_Edge(int fr,int to) { g[fr].push_back(to); }
void Tarjan(int u,int fa) {
int dfsn=++cnt;d[u]=cnt,ins[u]=1,stk[++top]=u;
for(int i=0,v;i<(int)g[u].size();i++) {
v=g[u][i];
if(!d[v]) Tarjan(v,u);
if(ins[v]) d[u]=min(d[u],d[v]);
}if(dfsn==d[u]) {
for(++cntb;stk[top]!=u;top--) {
b[stk[top]]=cntb,ins[stk[top]]=0;
}ins[stk[top]]=0,b[stk[top--]]=cntb;
}
// cout<<u<<":"<<dfsn<<" "<<d[u]<<endl;
}
int main() {
// freopen("in.in","r",stdin);
n=in(),m=in();
char opt[50];
for(int i=1;i<=m;i++) {
int u=in(),v=in(),w=in();
scanf("%s",opt);
if(u>v) swap(u,v);
if(opt[0]=='X') {
if(w) {
Add_Edge(u*2,v*2+1);
Add_Edge(u*2+1,v*2);
Add_Edge(v*2,u*2+1);
Add_Edge(v*2+1,u*2);
}else {
Add_Edge(u*2,v*2);
Add_Edge(v*2,u*2);
Add_Edge(u*2+1,v*2+1);
Add_Edge(v*2+1,u*2+1);
}
}else if(opt[0]=='A') {
if(w) {
Add_Edge(u*2,u*2+1);
Add_Edge(v*2,v*2+1);
}else {
Add_Edge(u*2+1,v*2);
Add_Edge(v*2+1,u*2);
}
}else {
if(w) {
Add_Edge(u*2,v*2+1);
Add_Edge(v*2,u*2+1);
}else {
Add_Edge(u*2+1,u*2);
Add_Edge(v*2+1,v*2);
}
}
}
for(int i=0;i<2*n;i++) if(!d[i]) Tarjan(i,i);
// for(int i=0;i<2*n;i++) cout<<b[i]<<" ";cout<<endl;
for(int i=0;i<2*n;i++) if(b[i]==b[i^1]) return puts("NO"),0;
return puts("YES"),0;
}

  

POJ 3678 Katu Puzzle的更多相关文章

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

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

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

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

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

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

  5. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

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

  7. poj 3678 Katu Puzzle(Two Sat)

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

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

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

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

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

随机推荐

  1. Centos6安装Gitlab

    安装参考 https://about.gitlab.com/downloads/ 可以从清华的镜像下载安装包, 注意区分自己用的是哪个发行版 https://mirror.tuna.tsinghua. ...

  2. java构造方法的作用以及简单java类

    public class TestDemo{ public static void main(String args[]){ Emp emp1 =new Emp(001,"tom" ...

  3. c#串口通信类代码可以直接调用

    文章首发于浩瀚先森博客 直接上代码 public struct SerialPara { private string portName; public string PortNameSetGet { ...

  4. 跟我从零基础学习Unity3D开发--U3d脚本注意事项及两个基本函数的简单介绍

    经过上一篇的学习,我相信开发环境您已经搭好了,如果还没有搭好可以百度一下. 今天主要讲的有以下几点: 第一:Unity3D中的C#脚本与.net中的C#写法的区别 1.Unity3D C#脚本不支持命 ...

  5. Android BLE 蓝牙编程(二)

    大家中秋快乐啊--哈哈,今天继续工程项目吧! 上篇我们已经实现了蓝牙设备的扫描,本篇我们来通过list展示扫描到的设备并 实现点击连接. 先贴出上篇的完整的MainActivity的方法: packa ...

  6. 解决:sudo: 无法解析主机:dinphy-500-310cn: 连接超时

    出现这种问题是hosts文件没有配置好所导致的,linux无法解析到您的主机地址,解决方案如下: sudo vim /etc/hosts 其中vim是你的文本编辑器的命令,你如果电脑中没有vim,用g ...

  7. SVN集中式版本控制器的安装、使用与常见问题汇总

    SVN是Subversion的简称,是一个开放源代码的版本控制系统,它采用了分支管理系统,集中式版本控制器 官方网站:https://www.visualsvn.com/ 下载右边的服务器端,左边的客 ...

  8. transform

    { transform: scale3d(x,y,z) /*放大*/ translate3d(x,y,z) /*位置*/ rotate3d(x,y,z,angle) /*旋转*/ skew(x-ang ...

  9. input-placeholder

    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #f00; } ::-moz-placeholder { /* Mozilla Fir ...

  10. base64和图片的转换

    /// <summary> /// base64转图片 /// </summary> /// <param name="strBase64">& ...