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]] + ...
随机推荐
- Spring Bean的一生
Spring Bean的一生 When you work directly in Java, you can do anything you like with your objects and do ...
- JAVA---spring-boot入门(图文教程)
Spring Boot可以轻松创建独立的,生产级的基于Spring的应用程序,他的特征: 1.创建独立的Spring应用程序 2.直接嵌入Tomcat,Jetty或Undertow(无需部 ...
- 【转】Maven项目中将配置文件打包到jar包中
参考博客:http://blog.csdn.net/ciedecem/article/details/10382275 问题: 项目中需要用到从文件中加载json数据,如图放在conf目录下. 程序中 ...
- ZR#331. 【18 提高 3】括号序列(栈)
题意 挺神仙的.首先$60$分暴力是比较好打的. 就是枚举左端点,看右端点能否是$0$ 但是这样肯定是过不了的,假如我们只枚举一次,把得到的栈记录下来 那么若区间$(l, r)$是可行的,那么$s_{ ...
- 20170405JDBC数据查询
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- Mac下安装ElasticSearch及其插件
目录 环境介绍 安装过程 安装Kibana 环境介绍 软件版本:ElasticSearch7.0.0 Kibana7.0.0 系统环境:mac 环境 安装过程 官网下载 ElasticSearch7. ...
- Linux Centos7.2 编译安装PHP7.0.2
操作环境: 1.系统:Centos7.2 2.服务:Nginx 1.下载PHP7.0.2的安装包解压,编译,安装: $ cd /usr/src/ $ wget http://cn2.php.net/d ...
- Classes and metaclasses
http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html Obje ...
- 1003: Redraiment的遭遇
1003: Redraiment的遭遇 Time Limit: 1000 Sec Memory Limit: 128 MBSubmit: 456 Solved: 158[Submit][Statu ...
- lca(最近公共祖先(离线))
转自大佬博客 : https://www.cnblogs.com/JVxie/p/4854719.html LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 首先是最近公共祖先 ...