2-sat(and,or,xor)poj3678
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7949 | Accepted: 2914 |
Description
Katu Puzzle is presented as a directed graph G(V, E) 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 ≤ Xi ≤ 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:
|
|
|
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 a (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的更多相关文章
- UVA - 12716 GCD XOR(GCD等于XOR)(数论)
题意:输入整数n(1<=n<=30000000),有多少对整数(a, b)满足:1<=b<=a<=n,且gcd(a,b)=a XOR b. 分析:因为c是a的约数,所以枚 ...
- DPLL 算法(求解k-SAT问题)详解(C++实现)
\(\text{By}\ \mathsf{Chesium}\) DPLL 算法,全称为 Davis-Putnam-Logemann-Loveland(戴维斯-普特南-洛吉曼-洛夫兰德)算法,是一种完备 ...
- 【机器学习】神经网络实现异或(XOR)
注:在吴恩达老师讲的[机器学习]课程中,最开始介绍神经网络的应用时就介绍了含有一个隐藏层的神经网络可以解决异或问题,而这是单层神经网络(也叫感知机)做不到了,当时就觉得非常神奇,之后就一直打算自己实现 ...
- 【BZOJ2337】Xor和路径(高斯消元)
[BZOJ2337]Xor和路径(高斯消元) 题面 BZOJ 题解 我应该多学点套路: 对于xor之类的位运算,要想到每一位拆开算贡献 所以,对于每一位拆开来看 好了,既然是按位来算 我们就只需要计算 ...
- 【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?
如何使用异或(XOR)运算找到数组中缺失的数? 今天给大家分享一篇关于使用XOR(异或)运算找到数组中缺失的数的问题. 在一次Javascript面试中,有这么一个问题: 假设有一个由0到99(包含9 ...
- UVa 12716 - GCD XOR(筛法 + 找规律)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 最大异或和(xor)
最大异或和(xor) 题目描述 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.A x:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Q l r x: ...
- 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, ...
- codeforces 617 E. XOR and Favorite Number(莫队算法)
题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...
随机推荐
- Locking
Computer Science An Overview _J. Glenn Brookshear _11th Edition To solve such problems, a DBMS could ...
- Machine Learning in Action – PCA和SVD
降维技术, 首先举的例子觉得很好,因为不知不觉中天天都在做着降维的工作 对于显示器显示一个图片是通过像素点0,1,比如对于分辨率1024×768的显示器,就需要1024×768个像素点的0,1来表示, ...
- ajax+jsp自动刷新
通过 AJAX,JavaScript 可使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信.通过这个对象, JavaScript 可在不重载页面的情况与 Web ...
- Oracle EBS 术语解释
SRS:是否在标准窗口进行提交请求 fnd_flex_value_sets.validation_type含义如下: D:从属I:独立N:无P:对U:特定F:表X:可转换独立Y:可转换从属
- Python创建list和按照索引访问list
Python创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素.比如,列出班里所有同学的名字,就可以用一个list表示:>> ...
- cvWaitKey 如果 cvNamedWindow就不会起作用
Have you called cvNamedWindow yet? It will not work without cvNamedWindow. http://stackoverflow.com/ ...
- 使用多种客户端消费WCF RestFul服务(一)——服务端
RestFul风格的WCF既然作为跨平台.跨语言.跨技术的一种方式出现,并且在ASP.NET API流行起来之前还是架构的首选技术之一,那么我们就来简要的介绍一下WCF在各个平台客户端的操作. 开发工 ...
- 小米1plus MIUI RadioButton的问题
小米1plus MIUI RadioButton不能设置setBackground(Drawable drawable);会变成黑色背景,需要单独处理
- JS-010-覆盖率测试工具 JSCoverage 初识
在日常的 js 脚本语言开发过程中,在开发攻城狮进行日常的单元测试和测试攻城狮日常的测试过程中,js 代码的覆盖率是白盒测试的一个重要的考量标准.前些天,在无意中看到了一个 js 覆盖率测试统计工具 ...
- SSH验证原理
http://www.tuicool.com/articles/qyiyim 下面会讲解ssh的密码登陆和免密码登陆.无论是密码登陆还是免密码登陆,安全使用的都是RSA非对称加密. SSH之所以能够保 ...