POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9987 | Accepted: 3741 |
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 Vi a 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
Hint
题目链接:POJ 3678
一开始不知道c是干嘛的,看了题解发现原来意思是 a op b = c,那这就很简单了,分3*2*2种情况讨论,因为每一种情况还要判断a是否等于b,如果等于的话加的边会不一样,甚至像$a xor a = 0$这种东西显然不存在的,可以直接判断NO了。其余的式子自己用化简成合取范式就OK。当然另外有一些时候化出来像$a \lor \lnot a=1$的,那直接就是1可以去掉不用考虑。
嗯做完这题基本可以告别2-SAT了,快做吐了……
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 2010;
const int M = 1000010 << 2;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
};
edge E[M];
int head[N], tot;
int dfn[N], low[N], st[N], belong[N], sc, ts, top;
bitset<N>ins;
int n, m; void init()
{
CLR(head, -1);
CLR(low, 0);
CLR(st, 0);
CLR(belong, 0);
sc = ts = top = 0;
ins.reset();
}
inline int rev(const int &k)
{
return k < n ? k + n : k - n;
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
void scc(int u)
{
dfn[u] = low[u] = ++ts;
ins[u] = 1;
st[top++] = u;
int i, v;
for (i = head[u]; ~i; i = E[i].nxt)
{
v = E[i].to;
if (!dfn[v])
{
scc(v);
low[u] = min(low[u], low[v]);
}
else if (ins[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
++sc;
do
{
v = st[--top];
ins[v] = 0;
belong[v] = sc;
} while (u != v);
}
}
int check()
{
for (int i = 0; i < (n << 1); ++i)
if (!dfn[i])
scc(i);
for (int i = 0; i < n; ++i)
if (belong[i] == belong[i + n])
return 0;
return 1;
}
int main(void)
{
int a, b, c, i;
char ops[10];
while (~scanf("%d%d", &n, &m))
{
init();
int flag = 1;
for (i = 0; i < m; ++i)
{
scanf("%d%d%d%s", &a, &b, &c, ops);
if (ops[0] == 'A')
{
if (c)
{
add(rev(a), a);
if (a != b)
add(rev(b), b);
}
else
{
if (a == b)
add(a, rev(a));
else
{
add(a, rev(b));
add(b, rev(a));
}
}
}
else if (ops[0] == 'O')
{
if (c)
{
if (a == b)
{
add(rev(a), a);
}
else
{
add(rev(a), b);
add(rev(b), a);
}
}
else
{
if (a == b)
add(a, rev(a));
else
{
add(a, rev(a));
add(b, rev(b));
}
}
}
else if (ops[0] == 'X')
{
if (c)
{
if (a == b)
flag = 0;
else
{
add(rev(a), b);
add(rev(b), a);
add(a, rev(b));
add(b, rev(a));
}
}
else
{
if (a == b)
;
else
{
add(a, b);
add(rev(b), rev(a));
add(rev(a), rev(b));
add(b, a);
}
}
}
}
puts((!flag || !check()) ? "NO" : "YES");
}
return 0;
}
POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)的更多相关文章
- poj 3678 Katu Puzzle(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
- 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 ...
- 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 ...
- POJ 3678 Katu Puzzle (经典2-Sat)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 2401 Descr ...
- POJ 3678 Katu Puzzle (2-SAT)
Katu Puzzle Time Limit: 1000MS ...
- 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 ...
- POJ 3678 Katu Puzzle 2-SAT 强连通分量 tarjan
http://poj.org/problem?id=3678 给m条连接两个点的边,每条边有一个权值0或1,有一个运算方式and.or或xor,要求和这条边相连的两个点经过边上的运算后的结果是边的权值 ...
- POJ 3678 Katu Puzzle
Description 给出一个关系,包括 And,Xor,Or 问是否存在解. Sol 经典的2-SAT问题. 把每个值看成两个点,一个点代表选 \(0\) ,另一个代表选 \(1\) . 首先来看 ...
- POJ 3678 Katu Puzzle(强连通 法)
题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...
随机推荐
- Python小脚本程序
本文旨在搜集最简单最原子性的代码块,简单清晰容易阅读,然后由用户自己组合.Python代码排版请自行修改. 这里是索引目录: 1. 下载网站文件 2. 下载网站多个文件 1. 下载网站文件 impor ...
- 学习sqlserver的函数方法
http://www.w3school.com.cn/sql/func_datediff.asp SQL Server DATEDIFF() 函数 SELECT DATEDIFF(day,'2008- ...
- 如何利用WordPress的菜单功能实现友情链接功能?
导语:对于wordpress网站的友情链接,有很多种方法可以实现,例如我们可以利用WordPress的链接功能,或者利用WordPress的菜单功能.本文章介绍的是如何利用菜单功能来实现友情链接. 前 ...
- Xcode Warning: “no rule to process file
警告⚠️: warning: no rule to process file '/Users/Kingdev/Desktop/Git/finance_iOS/finance/Library/MBpro ...
- React 服务端渲染最佳解决方案
最近在开发一个服务端渲染工具,通过一篇小文大致介绍下服务端渲染,和服务端渲染的方式方法.在此文后面有两中服务端渲染方式的构思,根据你对服务端渲染的利弊权衡,你会选择哪一种服务端渲染方式呢? 什么是服务 ...
- windows 解决缺失.dll的问题
1.缺失MSVCR120.dell和MSVCP120.dll,如图: 这种问题是因为没有Microsoft Visual C++ 2013运行库的问题,自行百度在Microsoft官网下载即可,注意需 ...
- Python_列表、字典、字符串、集合操作
一.list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素.对于list的操作,我们要学会增删改查. 查 我们可以直接索引查找,也可以通过切片 ...
- MYSQL 自定义排序
在mysql order by排序中,大多数情况下仅使用默认排序规则就够了:字符串按字典顺序,数字按大小等等.可有时候,某个字段是有自身业务含义的,比如 type(1,2,3)可能表示早/中/晚,如果 ...
- TCP/IP网络编程之优于select的epoll(二)
基于epoll的回声服务端 在TCP/IP网络编程之优于select的epoll(一)这一章中,我们介绍了epoll的相关函数,接下来给出基于epoll的回声服务端示例. echo_epollserv ...
- Python3.0-3.6的版本变化
Table of Contents Python3.0 简单的变化 语法的变化 新语法 改动的语法 剩下的变化 Python3.1 Python3.2 Python3.3 Python3.4 Pyth ...