题目链接: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缩点)的更多相关文章

  1. 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 ...

  2. poj3678 Katu Puzzle 2-SAT

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6714   Accepted: 2472 Descr ...

  3. 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 ...

  4. POJ3678:Katu Puzzle——题解

    http://poj.org/problem?id=3678 总觉得这题比例题简单. 设a为x取0的点,a+n为x取1的点. 我们还是定义a到b表示取a必须取b. 那么我们有: 当AND: 1.当c= ...

  5. 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 ...

  6. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

  7. POJ 3678 Katu Puzzle (经典2-Sat)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 2401 Descr ...

  8. 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 ...

  9. POJ 3678 Katu Puzzle

    Description 给出一个关系,包括 And,Xor,Or 问是否存在解. Sol 经典的2-SAT问题. 把每个值看成两个点,一个点代表选 \(0\) ,另一个代表选 \(1\) . 首先来看 ...

随机推荐

  1. Swift-闭包理解

    /* 闭包(Closures) * 闭包是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. * 在Swift中的闭包与C.OC中的blocks和其它编程语言(如Python)中的lambda ...

  2. 2018 杭电多校1 - Chiaki Sequence Revisited

    题目链接 Problem Description Chiaki is interested in an infinite sequence $$$a_1,a_2,a_3,...,$$$ which i ...

  3. BZOJ 1787 紧急集合(LCA)

    转换成抽象模型,就是要求一棵树(N个点,有N-1条边表示这个图是棵树)中某一点满足给定三点a,b,c到某一点的距离和最小.那么我们想到最近公共祖先的定义,推出只有集合点在LCA(a,b).LCA(a, ...

  4. java动态代理(JDK和CGLIB)笔记

    动态代理:为一堆interface或类的实现提供统一的执行通道,从含义上就像局域网电脑通过代理上网一样,走统一的通道,代理控制通道,自然可以在通道里加上自定义实现,例如像AOP切面,日志等. JDK的 ...

  5. [洛谷P4592][TJOI2018]异或

    题目大意:有一棵$n$个点的树,第$i$个点权值为$w_i$,有两种操作: $1\;x\;y:$询问节点$x$的子树中与$y$异或结果的最大值 $2\;x\;y\;z:$询问路径$x$到$y$上点与$ ...

  6. BZOJ1823:[JSOI2010]满汉全席——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1823 https://www.luogu.org/problemnew/show/P4171 题面 ...

  7. BZOJ2697 特技飞行 【贪心】

    题目链接 BZOJ2697 题解 好水好水的贪心... 容易发现每种特技只表演两次,多表演没有意义,而且差距越长收益越大 然后就可以贪,最大的放两端,次大的往里,然后是第三大....... 证明很简单 ...

  8. 专题训练之AC自动机

    推荐博客:http://www.cnblogs.com/kuangbin/p/3164106.html AC自动机小结 https://blog.csdn.net/creatorx/article/d ...

  9. [codeforces/gym/101350/L]维护“凸包”

    题目链接:http://codeforces.com/gym/101350/problems 给定n个墙,每个墙有一个高度,要支持动态修改墙的高度和查询这个“容器”能盛多少水. (队友)观察发现,能盛 ...

  10. [linux/net]策略路由实现特定主机特定路径

        echo 200 silence >> /etc/iproute2/rt_tables ip rule add from 10.192.0.230 table silence ip ...