LA 5010 Go Deeper 2-SAT 二分
题意:
有\(n\)个布尔变量\(x_i\),有一个递归函数。如果满足条件\(x[a[dep]] + x[b[dep]] \neq c[dep]\),那么就再往深递归一层。
问最多能递归多少层。
分析:
首先二分能递归的深度,然后在2-SAT中添加相应的约束条件。
约束条件是这样添加的,对于两个布尔变量\(x\)和\(y\):
- \(x+y \neq 0 \Rightarrow x \vee y\)
- \(x+y \neq 1 \Rightarrow \bar{x} \vee y, x \vee \bar{y}\)
- \(x+y \neq 2 \Rightarrow \bar{x} \vee \bar{y}\)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 200 + 10;
struct TwoSAT
{
int n;
vector<int> G[maxn * 2];
bool mark[maxn * 2];
int S[maxn * 2], c;
bool dfs(int x) {
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x] = true;
S[c++] = x;
for(int i = 0; i < G[x].size(); i++)
if(!dfs(G[x][i])) return false;
return true;
}
void init(int n) {
this->n = n;
for(int i = 0; i < n * 2; i++) G[i].clear();
memset(mark, false, sizeof(mark));
}
void add_clause(int x, int xval, int y, int yval) {
x = x * 2 + xval;
y = y * 2 + yval;
G[x^1].push_back(y);
G[y^1].push_back(x);
}
bool solve() {
for(int i = 0; i < n * 2; i += 2)
if(!mark[i] && !mark[i+1]) {
c = 0;
if(!dfs(i)) {
while(c > 0) mark[S[--c]] = false;
if(!dfs(i+1)) return false;
}
}
return true;
}
}solver;
const int maxm = 10000 + 10;
int n, m;
int a[maxm], b[maxm], c[maxm];
bool check(int dep) {
solver.init(n);
for(int i = 0; i < dep; i++) {
if(c[i] == 0) {
solver.add_clause(a[i], 1, b[i], 1);
}
else if(c[i] == 1) {
solver.add_clause(a[i], 1, b[i], 0);
solver.add_clause(a[i], 0, b[i], 1);
}
else solver.add_clause(a[i], 0, b[i], 0);
}
return solver.solve();
}
int main()
{
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++) scanf("%d%d%d", a + i, b + i, c + i);
int L = 0, R = m;
while(L < R) {
int mid = (L + R) / 2 + 1;
if(check(mid)) L = mid;
else R = mid - 1;
}
printf("%d\n", L);
}
return 0;
}
LA 5010 Go Deeper 2-SAT 二分的更多相关文章
- UVALive 5010 Go Deeper 2sat
二分答案,2sat判定. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio& ...
- BZOJ 1901 & 整体二分
题意: 带修改的区间第K小. SOL: 看了很久很久很久很久的整体二分,网上的各种题解也不是很多,也一直很不了解所谓的"贡献","将询问一起递归"是什么意思.. ...
- Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)
Go Deeper Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- LA 3211 飞机调度(2—SAT)
https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...
- LA 2678 Subsequence(二分查找)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 简单几何(半平面交+二分) LA 3890 Most Distant Point from the Sea
题目传送门 题意:凸多边形的小岛在海里,问岛上的点到海最远的距离. 分析:训练指南P279,二分答案,然后整个多边形往内部收缩,如果半平面交非空,那么这些点构成半平面,存在满足的点. /******* ...
- LA 4725 (二分) Airport
题意: 有W.E两个跑道,在每个时刻每个跑道的飞机都从0开始编号,而且每个时刻都有Wi和Ei架飞机到达这两个跑道.而且每个时刻只能选择一个跑道的一架飞机起飞.问如何选择才能使得飞机的最大编号最小.(每 ...
- LA 3971 (二分) Assemble
题意: 你有b块钱想要组装一台电脑.给出n个配件的种类,品质和价格,要求每个种类的配件各买一个总价格不超过b且“品质最差配件”的品质因子应尽量大. 这种情况下STL的map的确很好用,学习学习 这种最 ...
- zoj3422Go Deeper(2-sat + 二分)
题目请戳这里 题目大意: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + ...
随机推荐
- 时间日期相关:Date类、DateFormat类、Calendar类
1 Date类 类 Date 表示特定的瞬间,精确到毫秒. 1秒=1000毫秒 毫秒的0点:公元1970年 一月一日,午夜0:00:00 对应的毫秒值就是0 时间和日期的计算,必须依赖毫秒值. Sys ...
- Spring的七种事务传播机制
概述 当我们调用一个基于Spring的Service接口方法(如UserService#addUser())时,它将运行于Spring管理的事务环境中,Service接口方法可能会在内部调用其它的Se ...
- JS移动端浏览器取消右划后退的几种方法
在开发过程中,发现我们公司所使用的APP有点BUG,在APP中打开网页.H5应用之后,处于首页时,轻微的右划触发了后退事件,导致直接退出网页或者H5应用的页面,这样使得很多需要交互的手势没办法使用.本 ...
- ubuntu server 16.04安装GPU服务器
1 Ubuntu16.04 系统安装过程中,需要勾选openssh-server 方便远程连接 2 必须安装gcc 与g++ 3 安装显卡驱动 NVIDIA-Linux-x86_64-367.57.r ...
- Windows基础环境_安装配置教程(Windows7 64、JDK1.8、Android SDK23.0、TortoiseSVN 1.9.5)
Windows基础环境_安装配置教程(Windows7 64.JDK1.8.Android SDK23.0.TortoiseSVN 1.9.5) 安装包版本 1) JDK版本包 地址: htt ...
- AutoIt 脚本1
一.新建的AU3 脚本进行编辑 选择Edit Script 如果是相运行脚本可以用Run Script 如果是想将脚本编译成.exe 可以用Compile Script 1)一个简单的AU3脚本 Ma ...
- acdream 小晴天老师系列——我有一个数列! (ST算法)
小晴天老师系列——我有一个数列! Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)S ...
- java面试题(杨晓峰)---第八讲谈谈Vector,ArrayList,LinkedList有何区别?
Vector,ArrayList,LinkedList均为线性的数据结构,但是从现实方式与应用场景中又存在差别. 1 底层实现方式 ArrayList内部数组来实现,LinkedList内部采用双向链 ...
- UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)
题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 先预处理出每个元素之前和之后相同元素出现的位置,就可以在O(1)的 ...
- 2018.5.5 phpStorm破解2017.3版本方法
方法一 注册时,在打开的License Activation窗口中选择"License server",在输入框输入下面的网址: http://im.js.cn:8888 (新) ...