POJ 3678--Katu Puzzle(2-SAT)
感觉这题比较裸,表现出了2-sat的本质。
不过构图我想的还是太简单了,a&b=1我只连了 a1->b1,b1->a1,但其实是不对的。这样连,a0和b0可以同时选到。应该连a0->a1,b0->b1这样就能保证a0,b0都不被选到。或运算同理。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <bitset>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define pk(x) printf("%d\n", x)
using namespace std;
#define PI acos(-1.0)
#define EPS 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll; const int N = 1010;
const int M = 2000020; struct Edge {
int from, to, next;
} edge[M], edge2[M];
int head[N];
int cntE;
void addedge(int u, int v) {
edge[cntE].from = u; edge[cntE].to = v; edge[cntE].next = head[u]; head[u] = cntE++;
} int dfn[N], low[N], idx;
int stk[N], top;
int in[N];
int kind[N], cnt; void tarjan(int u)
{
dfn[u] = low[u] = ++idx;
in[u] = true;
stk[++top] = u;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
else if (in[v]) low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
++cnt;
while (1) {
int v = stk[top--]; kind[v] = cnt; in[v] = false;
if (v == u) break;
}
}
} bool sat(int n) // 序号从0开始
{
for (int i = 0; i < n; ++i) if (!dfn[i]) tarjan(i);
for (int i = 0; i < n; i += 2) {
if (kind[i] == kind[i+n]) return false;
}
return true;
} void init() {
memset(head, -1, sizeof head);
memset(dfn, 0, sizeof dfn);
memset(in, false, sizeof in);
cntE = idx = top = cnt = 0;
} int main()
{
int n, m;
int a, b, c;
char op[10];
while (~scanf("%d%d", &n, &m)) {
init();
while (m--) {
scanf("%d%d%d%s", &a, &b, &c, op); // 0 1
if (*op == 'A') {//and
if (c == 1) {
addedge(a, a+n);
addedge(b, b+n);
} else {
addedge(a+n, b);
addedge(b+n, a);
}
} else if (*op == 'X') {//xor
if (c == 1) { //1^0=1;
addedge(a+n, b);
addedge(b+n, a);
addedge(a, b+n);
addedge(b, a+n);
} else {
addedge(a+n, b+n);
addedge(b+n, a+n);
addedge(a, b);
addedge(b, a);
}
} else {//or 0|0=0
if (c == 1) {
addedge(a, b+n);
addedge(b, a+n);
} else {
addedge(a+n, a);
addedge(b+n, b);
}
}
}
if (sat(n)) puts("YES");
else puts("NO");
}
return 0;
}
POJ 3678--Katu Puzzle(2-SAT)的更多相关文章
- 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)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 2401 Descr ...
- POJ 3678 Katu Puzzle(强连通 法)
题目链接 题意:给出a, b, c 和操作类型 (与或异或),问是否满足所有的式子 主要是建图: 对于 and , c == 1: 说明 a 和 b都是1,那么 0 就不能取, a' -> a ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- POJ 3678 Katu Puzzle (2-SAT)
Katu Puzzle Time Limit: 1000MS ...
- POJ 3678 Katu Puzzle (2-SAT,常规)
题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) ...
- 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 建图入门
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(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
- POJ 3678 Katu Puzzle 2-SAT 强连通分量 tarjan
http://poj.org/problem?id=3678 给m条连接两个点的边,每条边有一个权值0或1,有一个运算方式and.or或xor,要求和这条边相连的两个点经过边上的运算后的结果是边的权值 ...
随机推荐
- linux 磁盘读写性能测试
1. 测试读取速度 haparm -Tt /dev/xxx 1.1 获取硬盘设备名称: fdisk -l Disk /dev/xvdf: 365.0 GB, 365041287168 bytes 25 ...
- [译]C++书籍终极推荐
转载声明: 翻译仅以技术学习和交流为目的,如需转载请务必标明原帖链接. 来源:http://stackoverflow.com/questions/388242/the-definitive-c-bo ...
- Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤
原文地址: Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤 - 网络资源是无限的 - 博客频道 - CSDN.NET http://blog.csdn.net/fen ...
- Android Editext监听光标位置
因为项目需要,需要实时监听光标的位置变化,网上提出的用TextWatcher和onTouchListener中调用contentText.getSelectionStart()都是获取的上一次位置. ...
- JavaScript定时器详解及实例
JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操作. setTimeout("function& ...
- R语言中的箱图介绍 boxplot
画箱图的函数: boxplot()##help(boxplot)查询具体用法 图例的解释: 如下图,是两个简单的箱图. 中间的箱子的上下边,分别是第三,一个四分位数. 中间的黑线是第二四分位数(中 ...
- String.IndexOf String.IndexOf String.Substring
String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. ...
- C/C++中static关键词的作用
1.在函数体内的static变量作用范围是该函数体,其只被内存分配一次,所以在下次调用的时候会保持上一次的值. 2.模块内的static全局变量可以被模块内的所有函数访问,但不能被模块外的函数访问. ...
- poj3216
这是一道描述非常不清楚的题目 首先解释一下,题目中的ti是任务开始时间不是结束时间, 然后维修人员可以理解为可以再任意时间从公司出发: 好,首先不难想到用floyd预处理一下: 然后我们把每个任务看成 ...
- bzoj1433:[ZJOI2009]假期的宿舍
明显的二分图最大匹配. #include<cstdio> #include<cstring> #include<cctype> #include<algori ...