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]] + ...
随机推荐
- E. Karen and Supermarket
E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- 【复习笔记】HTML基础
编码 HTML LANG标注整体文档语言 常用编码:ASCII.GB2312.UTF-8 中文编码解决: 1.浏览器要用一个编码表去看你的文件<meta charset="utf-8& ...
- [LoadRunner]录制启动时报“The JVM could not be started……”错误解决方案
在LR准备点击录制java over http协议时,程序报如下错误: 报错提示是设置的JVM值设置问题,导致不能启动. 解决方案一 点击F4快捷按钮,会弹出以下界面,在选中的位置选择对应的java路 ...
- cmd_ping命令
ping命令是网络命令里的核心命令,同时也是黑客入侵的基础命令.下面和大家分享一下ping命令的基础知识和一般用法. 以ping百度公司域名为例,介绍ping命令相关内容. 一.ping命令基础知识 ...
- MySQL基础环境_安装配置教程(Windows7 64或Centos7.2 64、MySQL5.7)
MySQL基础环境_安装配置教程(Windows7 64或Centos7.2 64.MySQL5.7) 安装包版本 1) VMawre-workstation版本包 地址: https://m ...
- vue分环境打包配置不同命令
1.安装cross-env (cross-env能跨平台地设置及使用环境变量)cnpm/npm i cross-env -D 2.新建模板 红色的为相关文件 3.配置各个文件 (1)config下 ...
- WAMP安装提示缺少 msvcr100.dll文件解决方法
WAMP安装提示缺少wamp msvcr100.dll文件解决方法 因为wamp基于vs c++2010开发,需要提前安装这个组件才可以正常运行 微软官方组件下载地址: 32位:http://www. ...
- UI EventSystem事件监听
Unity5.0 EventSystem事件系统的详细说明 一.EventSystem对象的说明 当我们在场景中创建任一UI对象后,Hierarchy面板中都可以看到系统自动创建了对象EventSys ...
- CF Gym 100463B Music Mess (思路)
好题,当时想了半个小时,我往图论方面去想了,把出现过的字符串当场点,然后相互连边,那么就构成了一个三角形,一个大于三个点的连通分量里有以下结论:度为二的点可能是track,度为大于二的点一定不是tra ...
- python_93_面向对象实例2
class Role: def __init__(self,name,role,weapon,life_value=100,money=15000): '构造函数:实例化时做一些类的初始化工作' se ...