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. Structured Streaming Programming Guide

    https://spark.apache.org/docs/latest/structured-streaming-programming-guide.html http://www.slidesha ...

  2. Python的深拷贝与浅拷贝

    一.前奏:熟悉Python内存管理 在Python中,变量在第一次赋值时自动声明,在创建—也就是赋值的时候,解释器会根据语法和右侧的操作数来决定新对象的类型. 引用计数器:一个内部跟踪变量 引用计数: ...

  3. angularjs select 循环中出现第一个 option 为空格问题

    当select 的ng-module 为空时, select显示空白行. 解决:指定ng-module的默认值.

  4. VirtualBox 使用技巧

    一.VirtualBox 的快捷键 VirtualBox 默认的 Host 键是 Right Ctrl Host 键可以点击 Oracle VM VirtualBox 管理器的左上角 “管理”-> ...

  5. 一张表有三个字段:id(城市id) Cityname(城市名) Privence(所属省份)如果要统计每个省份有多少城市请用SQL实现。

    一张表有三个字段:id(城市id) Cityname(城市名) Privence(所属省份)如果要统计每个省份有多少城市请用SQL实现.

  6. 20145211 《Java程序设计》实验报告二:Java面向对象程序设计

    实验要求 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验内容 单元测试 面向对象三要素 设计模式初步 练习 实 ...

  7. xml架构管理器

    http://technet.microsoft.com/zh-cn/dd489278

  8. Linux makefile 教程 非常详细,且易懂 (转)

    概述—— 什么是makefile?或许很多Winodws的程序员都不知道这 个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makef ...

  9. iOS 冒泡排序

    //冒泡排序 -(NSArray*)Bubble_Sort:(NSArray*)oldArray { NSMutableArray * newArray = [NSMutableArray array ...

  10. 圆角边框_css控制形状

    border-radius:500px 来让整个图像变成圆形. border-top-left-radius: 6px;(左上角圆角)   border-top-right-radius: 6px;( ...