题意:

有\(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 二分的更多相关文章

  1. UVALive 5010 Go Deeper 2sat

    二分答案,2sat判定. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio& ...

  2. BZOJ 1901 & 整体二分

    题意: 带修改的区间第K小. SOL: 看了很久很久很久很久的整体二分,网上的各种题解也不是很多,也一直很不了解所谓的"贡献","将询问一起递归"是什么意思.. ...

  3. Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)

    Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. LA 3211 飞机调度(2—SAT)

    https://vjudge.net/problem/UVALive-3211 题意: 有n架飞机需要着陆,每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种,第i架飞机的早着陆时间 ...

  5. LA 2678 Subsequence(二分查找)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. 简单几何(半平面交+二分) LA 3890 Most Distant Point from the Sea

    题目传送门 题意:凸多边形的小岛在海里,问岛上的点到海最远的距离. 分析:训练指南P279,二分答案,然后整个多边形往内部收缩,如果半平面交非空,那么这些点构成半平面,存在满足的点. /******* ...

  7. LA 4725 (二分) Airport

    题意: 有W.E两个跑道,在每个时刻每个跑道的飞机都从0开始编号,而且每个时刻都有Wi和Ei架飞机到达这两个跑道.而且每个时刻只能选择一个跑道的一架飞机起飞.问如何选择才能使得飞机的最大编号最小.(每 ...

  8. LA 3971 (二分) Assemble

    题意: 你有b块钱想要组装一台电脑.给出n个配件的种类,品质和价格,要求每个种类的配件各买一个总价格不超过b且“品质最差配件”的品质因子应尽量大. 这种情况下STL的map的确很好用,学习学习 这种最 ...

  9. zoj3422Go Deeper(2-sat + 二分)

    题目请戳这里 题目大意: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + ...

随机推荐

  1. P4868 天天和不可描述

    http://www.tyvj.cn/p/4868 思路: 本想用站做的,但发现要用很多站同时做,还要来回倒. 我怕超时,所以换了种做法. 因为每遇到一次括号都要把输出方向改变,而括号是成对存在的,所 ...

  2. 1126 数字统计 2010年NOIP全国联赛普及组

    1126 数字统计 2010年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver         题目描述 Description 请统计某个 ...

  3. The great pleasure in life is doing what people say you cannot do.

    The great pleasure in life is doing what people say you cannot do.  人生最大的快乐是做到别人认为你做不到的事情.

  4. 【简抄】h5 新增的几个背景属性和文本属性

    一.背景图像显示: ①background-size:规定背景图像的大小: 值:像素值.百分比.auto.cover.contain ②background-origin :规定背景图像的起始位置: ...

  5. Chrome Java插件过期

    企业应用软件中,基本都是基于某个版本的JDK进行开发的,更新跟不上Oracle更新的步伐,Chrome浏览器自动默认关闭了过期插件导致用Chrome无法打开应用软件. 解决办法如下:

  6. Win10自动安装游戏/应用

    近日有一些网友反馈Win10会自动安装一些游戏/应用.用户没有打开应用商店并且没有被要求获取批准,系统自动在系统中安装了一些应用. 比如有这样一位网友反馈了该问题,他在自己的系统中使用命令卸载了所有的 ...

  7. win7下一直试用Beyond Compare 4

    找到目录C:\Users\用户名\AppData\Roaming\BeyondCompare,将这个目录删除,重启compare即可.

  8. Connectivity

    6492: Connectivity 时间限制: 1 Sec  内存限制: 128 MB提交: 118  解决: 28[提交][状态][讨论版][命题人:admin] 题目描述 There are N ...

  9. rcnn,sppnet,fast rcnn,ohem,faster rcnn,rfcn

    https://zhuanlan.zhihu.com/p/21412911 rcnn需要固定图片的大小,fast rcnn不需要 rcnn,sppnet,fast rcnn,ohem,faster r ...

  10. mtDNA|ctDNA|cpDNA|

    5.9细胞器基因组是编码细胞器蛋白质的环状DNA分子 细胞器中除真核细胞线粒体DNA(mtDNA)是线性的外,都是环状分子,比如叶绿体DNA(ctDNA,cpDNA).因为单个细胞器有几套不同拷贝的细 ...