Katu Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7949   Accepted: 2914

Description

Katu Puzzle is presented as a directed graph G(VE) 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 Via value Xi (0 ≤ X≤ 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:

AND 0 1
0 0 0
1 0 1
OR 0 1
0 0 1
1 1 1
XOR 0 1
0 0 1
1 1 0

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 (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
题意:给出一个连通图对于每条边都有一种操作(and,or,xor),使两个端点的操作结果是c,问是否存在这样一个连通图,存在输出YES否者输出NO
分析:裸的2-sat操作
公式:i&j=1 (i-->i+n) (j-->j+n)
i&j=0 (i+n-->j) (j+n-->i)
i|j=1 (i-->j+n) (j-->i+n)
i|j=0 (i+n-->i) (j+n-->j)
i^j=1 (i-->j+n) (j+n-->i) (j-->i+n) (i+n-->j)
i^j=0 (i-->j) (j-->i) (i+n-->j+n) (j+n-->i+n)
程序;
#include"string.h"
#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#include"stdlib.h"
#include"math.h"
#define inf 10000000
#define INF 0x3f3f3f3f
const double PI=acos(-1.0);
const double r2=sqrt(2.0);
const int M=1010;
const int N=1010*1000*4;
#define eps 1e-10
using namespace std;
struct node
{
int u,v,next;
}edge[N];
stack<int>q;
int t,head[M],dfn[M],low[M],belong[M],use[M],num,index;
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
void tarjan(int u)
{
dfn[u]=low[u]=++index;
q.push(u);
use[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(use[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
num++;
int p;
do
{
p=q.top();
q.pop();
use[p]=0;
belong[p]=num;
}while(p!=u);
}
}
int slove(int n)
{
num=index=0;
memset(use,0,sizeof(use));
memset(dfn,0,sizeof(dfn));
for(int i=0;i<n*2;i++)
if(!dfn[i])
tarjan(i);
for(int i=0;i<n;i++)
if(belong[i]==belong[i+n])
return 0;
return 1;
}
int main()
{
int n,m,a,b,c,i;
char str[9];
while(scanf("%d%d",&n,&m)!=-1)
{
init();
for(i=1;i<=m;i++)
{
scanf("%d%d%d%s",&a,&b,&c,str);
if(strcmp(str,"AND")==0)
{
if(c)
{
add(a,a+n);
add(b,b+n);
}
else
{
add(b+n,a);
add(a+n,b);
}
}
else if(strcmp(str,"OR")==0)
{
if(c)
{
add(b,a+n);
add(a,b+n);
}
else
{
add(a+n,a);
add(b+n,b);
}
}
else
{
if(c)
{
add(a,b+n);
add(b,a+n);
add(b+n,a);
add(a+n,b);
}
else
{
add(a,b);
add(b,a);
add(a+n,b+n);
add(b+n,a+n);
}
}
}
if(slove(n))
printf("YES\n");
else
printf("NO\n");
}
}

2-sat(and,or,xor)poj3678的更多相关文章

  1. UVA - 12716 GCD XOR(GCD等于XOR)(数论)

    题意:输入整数n(1<=n<=30000000),有多少对整数(a, b)满足:1<=b<=a<=n,且gcd(a,b)=a XOR b. 分析:因为c是a的约数,所以枚 ...

  2. DPLL 算法(求解k-SAT问题)详解(C++实现)

    \(\text{By}\ \mathsf{Chesium}\) DPLL 算法,全称为 Davis-Putnam-Logemann-Loveland(戴维斯-普特南-洛吉曼-洛夫兰德)算法,是一种完备 ...

  3. 【机器学习】神经网络实现异或(XOR)

    注:在吴恩达老师讲的[机器学习]课程中,最开始介绍神经网络的应用时就介绍了含有一个隐藏层的神经网络可以解决异或问题,而这是单层神经网络(也叫感知机)做不到了,当时就觉得非常神奇,之后就一直打算自己实现 ...

  4. 【BZOJ2337】Xor和路径(高斯消元)

    [BZOJ2337]Xor和路径(高斯消元) 题面 BZOJ 题解 我应该多学点套路: 对于xor之类的位运算,要想到每一位拆开算贡献 所以,对于每一位拆开来看 好了,既然是按位来算 我们就只需要计算 ...

  5. 【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?

    如何使用异或(XOR)运算找到数组中缺失的数? 今天给大家分享一篇关于使用XOR(异或)运算找到数组中缺失的数的问题. 在一次Javascript面试中,有这么一个问题: 假设有一个由0到99(包含9 ...

  6. UVa 12716 - GCD XOR(筛法 + 找规律)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. 最大异或和(xor)

    最大异或和(xor) 题目描述 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.A x:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Q l r x: ...

  8. CF 979D Kuro and GCD and XOR and SUM(异或 Trie)

    CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...

  9. codeforces 617 E. XOR and Favorite Number(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...

随机推荐

  1. Python - KMP算法

    def kmp_match(tex, pat): n = len(tex) m = len(pat) tex = '0' + tex pat = '0' + pat pi = [] pi.append ...

  2. JS中的工厂模式

    .一个栗子: var BicycleShop = function(){}; BicycleShop.prototype = { sellBicycle : function( model ){ va ...

  3. 出现upstream sent too big header while reading response header from upstream错误

    一个POS系统,出现upstream sent too big header while reading response header from upstream错误. 1.反向代理端,可以放到se ...

  4. 通过宏定义判断是否引入的是framework,反之则使用双引号,实用!

    例: #if __has_include(<TestHead/TestHead.h>) #import <TestHead/TestHead.h>#else#import &q ...

  5. 【C51】单片机独立按键与矩阵按键

    独立按键 首先既然是检测输入,对于当然要用到拉电阻,来检测引脚电平变化变化.51单片机中,除了P0口外,P2,P3,P4都是内置上拉电阻的准双向IO口,一般 的 51 P0引脚都外接了上拉电阻,当然也 ...

  6. DevExpress的所有功能介绍

    https://www.devexpress.com/Subscriptions/New-2016-2.xml?utm_source=AnnounceTry&utm_medium=WhatsN ...

  7. 在Windows上一键编译各种版本的Protobuf

    所需工具 : cmake  for  windows 和  git for windows 原理:protobuf 是google的一个开源项目,其源代码在github上可以下载到,并且源码都采用cm ...

  8. 消息队列系列(一):.Net平台下的消息队列介绍

    本系列主要记录最近学习消息队列的一些心得体会,打算形成一个系列文档.开篇主要介绍一下.Net平台下一些主流的消息队列框架.       RabbitMQ:http://www.rabbitmq.com ...

  9. Ubuntu deb包使用

    deb是debian linus的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian Package的简写,是为Debian 专 ...

  10. 通过css3实现开关选择按钮

      通过css属性实现如图所示按钮 要点:通过checkbox选中与否的状态与兄弟选择器实现相关功能 1.设置开关大小并设置定位方式为relative .swift-btn {     positio ...