Katu Puzzle(POJ3678+2-SAT问题+tarjan缩点)
题目链接:http://poj.org/problem?id=3678
题目:


题意:给你a,b,c,op,op为逻辑运算符或、与、异或,使得a op b = c,让你判断这些运算符是否存在矛盾,不存在输出YES,存在输出NO。
思路:2-SAT问题。2-SAT问题一般都是每个节点有两种选择,并且在节点中间将存在一定的限制,譬如a为1,那么b必须为1或a为0,b必须为1……而且当一个命题存在时,它的逆否命题必然存在(此处由命题为真,则其逆否命题也为真得证)。我们通过将这些关系转换成有向的边,通过tarjan缩点,我们可以通过判断同一个节点是否它的两种选择在同一个SCC中来决定是否存在矛盾。此题我们假设i为i节点取1,i+n为i节点取0,然后对c和op进行分类讨论,进行建图跑tarjan,从而解决此题。
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, m, a, b, c, tot, cnt, num, top;
char op[];
int head[<<];
int vis[<<], dfn[<<], low[<<], stc[<<], p[<<]; struct edge {
int v, next;
}ed[maxn<<]; void addedge(int u, int v) {
ed[tot].v = v;
ed[tot].next = head[u];
head[u] = tot++;
} void tarjan(int x) {
dfn[x] = low[x] = ++num;
stc[++top] = x, vis[x] = ;
for(int i = head[x]; ~i; i = ed[i].next) {
int y = ed[i].v;
if(!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if(vis[y]) {
low[x] = min(low[x], low[y]);
}
}
if(dfn[x] == low[x]) {
int y; cnt++;
do {
y = stc[top--], vis[y] = ;
p[y] = cnt;
} while(x != y);
}
} int main() {
//FIN;
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
for(int i = ; i <= m; i++) {
scanf("%d%d%d%s", &a, &b, &c, op);
if(op[] == 'A') {
if(c == ) {
addedge(a + n, a);
addedge(b + n, b);
} else {
addedge(a, b + n);
addedge(b, a + n);
}
} else if(op[] == 'O') {
if(c == ) {
addedge(a + n, b);
addedge(b + n, a);
} else {
addedge(a, a + n);
addedge(b, b + n);
}
} else {
if(c == ) {
addedge(a, b + n);
addedge(b, a + n);
addedge(a + n, b);
addedge(b + n, a);
} else {
addedge(a, b);
addedge(b, a);
addedge(a + n, b + n);
addedge(b + n, a + n);
}
}
}
for(int i = ; i < * n; i++) {
if(!dfn[i]) {
tarjan(i);
}
}
int flag = ;
for(int i = ; i < n; i++) {
if(p[i] == p[i+n]) {
flag = ;
break;
}
}
if(flag) puts("YES");
else puts("NO");
return ;
}
Katu Puzzle(POJ3678+2-SAT问题+tarjan缩点)的更多相关文章
- 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 ...
- poj3678 Katu Puzzle 2-SAT
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6714 Accepted: 2472 Descr ...
- POJ3678 Katu Puzzle 【2-sat】
题目 Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a boolean ...
- POJ3678:Katu Puzzle——题解
http://poj.org/problem?id=3678 总觉得这题比例题简单. 设a为x取0的点,a+n为x取1的点. 我们还是定义a到b表示取a必须取b. 那么我们有: 当AND: 1.当c= ...
- 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 ...
- 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 建图入门
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
Description 给出一个关系,包括 And,Xor,Or 问是否存在解. Sol 经典的2-SAT问题. 把每个值看成两个点,一个点代表选 \(0\) ,另一个代表选 \(1\) . 首先来看 ...
随机推荐
- iOS AVAudioPlayer播放音频时声音太小
iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...
- Jenkins系列-Jenkins忘记密码的修复方法
找回 admin 用户的密码后,可以登录系统修改其他用户的密码. 1. Jenkins 目录结构 Jenkins 没有使用数据库,所有的信息都保存在 JENKINS_HOME 目录下的文件中.其中 J ...
- ASP.NET MVC 多语言解决方案
1:打开VS,新建ASP.NET MVC4项目 2:创建一个放本地化资源的文件夹并命名为"Language",右键选择添加新项,选择资源文件并命名为"Com" ...
- WebKit 源码分析 -- loader
原文地址: http://peirenlei.iteye.com/blog/1718569 摘要:本文介绍 WebCore 中 Loader 模块是如何加载资源的,分主资源和派生资源分析 loader ...
- 关于new delete的说明
1. 删除空指针不会有问题,因为C++的标准规定在delete时首先会判断指针是否为空,为空就不再处理,所以也就不会有问题. 2. delete一个非空指针之后,并不会将该指针自动置为空.此时如果重复 ...
- 钉钉 E应用 打开分享外链
钉钉 E应用 打开分享外链 外部链接 https://open-doc.dingtalk.com/microapp/dev https://open-doc.dingtalk.com/microapp ...
- BZOJ 1179 Atm(强连通分量缩点+DP)
题目说可以通过一条边多次,且点权是非负的,所以如果走到图中的一个强连通分量,那么一定可以拿完这个强连通分量上的money. 所以缩点已经很明显了.缩完点之后图就是一个DAG,对于DAG可以用DP来求出 ...
- BZOJ 1046 上升序列(LIS变形)
要保证长度为L的序列下标字典序最小,当然要尽量选前面的数. 如何判断前面的数是否满足条件?,只需要知道这个数开头的递增序列的最长长度是多少,如果不小于L,那么必然可以加入这个数.还需判断一下它是否大于 ...
- CF995A Tesla
题目描述 Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He kno ...
- 【题解】HNOI2016网络
整体二分是个好东西!可我忘记了它QAQ其实当你知道这题可以整体二分的时候就已经不难了(个人觉得这是最难想到的一点啊).整体二分的话,我们就可以把问题转化为是否有一条权值 \(>= k\) 的链经 ...