POJ 3678 Katu Puzzle (2-SAT,常规)
题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定。问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定)
思路:题意很清晰了,难点在建图。要考虑所有可能的冲突:
当op为and: (1)c为0时,其中1个必为0。
(2)c为1时,两者必为1。要加两条边,形如 a0->a1。
当op为or: (1)c为0时,两者必为0。要加两条边,形如 a1->a0。
(2)c为1时,其中1个必为1。
当op为xor: (1)c为0时,两者必定相同。
(2)c为1时,两者必定不同。
都是按照冲突来建图就行,有没有解留给DFS去判定。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
//#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=*+;
int res[N][N];
vector<int> vect[N*];
map<string,int> mapp; void init()
{
string tmp="AND";
mapp[tmp]=;
tmp="OR";
mapp[tmp]=;
tmp="XOR";
mapp[tmp]=;
}
int s[N*], col[N*], c;
bool color(int x)
{
if(col[x^]) return false;
if(col[x]) return true;
col[x]=;
s[c++]=x;
for(int i=; i<vect[x].size(); i++)
if(!color(vect[x][i])) return false;
return true;
} int cal(int n)
{
memset(col,,sizeof(col));
memset(s,,sizeof(s));
for(int i=; i<n; i+=)
{
if(!col[i]&&!col[i+])
{
c=;
if(!color(i))
{
while(c) col[s[--c]]=;
if(!color(i+)) return false;
}
}
}
return true;
} int main()
{
freopen("input.txt", "r", stdin);
init();
string op;
int n, m, a, b, c;
while(~scanf("%d%d",&n,&m))
{
memset(res,0xf0,sizeof(res));
for(int i=n*; i>=; i--) vect[i].clear(); for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
cin>>op;
res[a][b]=c;
int t=mapp[op];
//设i*2为0,i*2+1为1
if(t==) //and
{
if(c==) //其中必有1个为0
{
vect[a*+].push_back(b*);
vect[b*+].push_back(a*); }
else //两者必为1
{
vect[a*].push_back(a*+); //指向自己
vect[b*].push_back(b*+);
}
}
else if(t==) //or
{
if(c==) //两者必为0
{
vect[a*+].push_back(a*);
vect[b*+].push_back(b*);
}
else //其中必有1个为1
{
vect[a*].push_back(b*+);
vect[b*].push_back(a*+);
}
}
else //XOR
{
if(c==) //两者必定相同
{
vect[a*].push_back(b*);
vect[b*].push_back(a*);
vect[a*+].push_back(b*+);
vect[b*+].push_back(a*+);
}
else //两者必定不同
{
vect[a*].push_back(b*+);
vect[a*+].push_back(b*);
vect[b*].push_back(a*+);
vect[b*+].push_back(a*);
}
}
}
if(!cal(n<<)) puts("NO");
else puts("YES");
}
return ;
}
AC代码
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,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- 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\) . 首先来看 ...
随机推荐
- Spark Streaming揭秘 Day16 数据清理机制
Spark Streaming揭秘 Day16 数据清理机制 今天主要来讲下Spark的数据清理机制,我们都知道,Spark是运行在jvm上的,虽然jvm本身就有对象的自动回收工作,但是,如果自己不进 ...
- 用minicom 产看 usb的串口
1 用命令 sudo apt-get install minicom 安装 2 用 minicom -s 进行配置 往下选择 Seral port setup: 然后输入 A :选择自己的 ...
- 使用OpenXml操作Excel,以下方法用于在添加列时修改Cell的CellReference属性。
以下方法实现了递增Excel中单元格的CellReference的功能,只支持两位字母. public static string CellReferenceIncrement(string cell ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- 零基础学Python 3之环境准备
一. 配置python 3 环境 1. Python 3 下载 64位 https://www.python.org/ftp/python/3.4.2/python-3.4.2.amd64.msi 3 ...
- 2329: [HNOI2011]括号修复 - BZOJ
恶心的splay,打标记的时候还有冲突,要特别小心 上次写完了,查了半天没查出错来,于是放弃 今天对着标程打代码,终于抄完了,我已经不想再写了 const maxn=; type node=recor ...
- 纯CSS制作二级导航
一.问题描述 做一个类似校园网首页,主要是导航栏的设置,ul默认纵向排列,如何横向排列,同时去掉圆点. 二.问题解决 2.1 先写导航条 用两个ul嵌套,一个ul是横向导航条,另一个是每个小项目下连一 ...
- SDK更新问题解决
更新C:\WINDOWS\system32\drivers\etc\host文件百试不爽 第一步 打开SDK Manager下Tools->Options,选中“Force https://… ...
- 一分钟明白 VS manifest 原理
什么是vs 程序的manifest文件 manifest 是VS程序用来标明所依赖的side-by-side组建,如ATL, CRT等的清单. 为什么要有manifest文件 一台pc上,用一组建往往 ...
- DJANGO技巧两则:模拟MKDIR -P及配合NGINX上传大文件不使超时
这都是在开发当哪遇到的问题,网上转转,作个记录: http://blog.chinaunix.net/uid-25525723-id-1596574.html http://bookshadow.co ...