题目请戳这里

题目大意:

go(int dep, int n, int m)
begin
output the value of dep.
if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep + 1, n, m)
end

读上面程序段,yy出函数功能。数组a,b,c长度为m,x长度为n。数组a,b中元素范围[0,n - 1],数组c元素为0或1或2。x数组元素为1或0。求能输出的最大的m。

题目分析:2-sat,还是比较裸的吧。要求最大的m,所以对长度m二分。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 405;
const int M = 10005; struct node
{
int to,next;
}g[M];
int head[N],stack1[N],stack2[N],vis[N],scc[N];
int n,m,num;
bool flag;
int a[M],b[M],c[M];
void init()
{
memset(head,-1,sizeof(head));
flag = true;
memset(vis,0,sizeof(vis));
memset(scc,0,sizeof(scc));
stack1[0] = stack2[0] = 0;
num = 0;
}
void build(int s,int e)
{
g[num].to = e;
g[num].next = head[s];
head[s] = num ++;
}
void dfs(int cur,int &sig,int &cnt)
{
if(flag == false)
return;
vis[cur] = ++ sig;
stack1[++stack1[0]] = cur;
stack2[++stack2[0]] = cur;
for(int i = head[cur];~i;i = g[i].next)
{
if(!vis[g[i].to])
dfs(g[i].to,sig,cnt);
else
{
if(scc[g[i].to] == 0)
while(vis[stack2[stack2[0]]] > vis[g[i].to])
stack2[0] --;
}
}
if(stack2[stack2[0]] == cur)
{
stack2[0] --;
cnt ++;
do
{
scc[stack1[stack1[0]]] = cnt;
if(scc[stack1[stack1[0]]^1] == cnt)
{
flag = false;
return;
}
}while(stack1[stack1[0] --] != cur);
}
}
void Gabow()
{
int i,sig,cnt;
sig = cnt = 0;
for(i = 0;i < n + n && flag;i ++)
if(!vis[i])
dfs(i,sig,cnt);
}
void solve()
{
int l,r,mid;
int ans,i;
l = 0;r = m;
while(l <= r)
{
mid = (l + r)>>1;
init();
for(i = 0;i < mid;i ++)
{
int u = a[i]<<1;
int v = b[i]<<1;
if(c[i] == 0)// !=0
{
build(u,v^1);
build(v,u^1);
}
if(c[i] == 1)// != 1
{
build(u,v);
build(v,u);
build(u^1,v^1);
build(v^1,u^1);
}
if(c[i] == 2)// != 2
{
build(u^1,v);
build(v^1,u);
}
}
Gabow();
if(flag)
{
ans = mid;
l = mid + 1;
}
else
r = mid - 1;
}
printf("%d\n",ans);
}
int main()
{
int i,t;
scanf("%d",&t);
while(t --)
{
scanf("%d%d",&n,&m);
for(i = 0;i < m;i ++)
scanf("%d%d%d",&a[i],&b[i],&c[i]);
solve();
}
return 0;
}

zoj3422Go Deeper(2-sat + 二分)的更多相关文章

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

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

  2. LA 5010 Go Deeper 2-SAT 二分

    题意: 有\(n\)个布尔变量\(x_i\),有一个递归函数.如果满足条件\(x[a[dep]] + x[b[dep]] \neq c[dep]\),那么就再往深递归一层. 问最多能递归多少层. 分析 ...

  3. hdu3715 Go Deeper[二分+2-SAT]/poj2723 Get Luffy Out[二分+2-SAT]

    这题转化一下题意就是给一堆形如$a_i + a_j \ne c\quad (a_i\in [0,1],c\in [0,2])$的限制,问从开头开始最多到哪条限制全是有解的. 那么,首先有可二分性,所以 ...

  4. hdu3715 2-sat+二分

    Go Deeper 题意:确定一个0/1数组(size:n)使得满足最多的条件数.条件在数组a,b,c给出. 吐槽:哎,一水提,还搞了很久!关键是抽象出题目模型(如上的一句话).以后做二sat:有哪些 ...

  5. Go Deeper(2010成都现场赛题)(2-sat)

    G - Go Deeper Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description ...

  6. HDU 3715 Go Deeper(2-sat)

    HDU 3715 Go Deeper 题目链接 题意:依据题意那个函数,构造x数组.问最大能递归层数 思路:转化为2-sat问题,因为x仅仅能是0.1,c仅仅能是0,1.2那么问题就好办了,对于0, ...

  7. Go Deeper

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

  8. 证明与计算(3): 二分决策图(Binary Decision Diagram, BDD)

    0x01 布尔代数(Boolean algebra) 大名鼎鼎鼎的stephen wolfram在2015年的时候写了一篇介绍George Boole的文章:George Boole: A 200-Y ...

  9. Map Labeler POJ - 2296(2 - sat 具体关系建边)

    题意: 给出n个点  让求这n个点所能建成的正方形的最大边长,要求不覆盖,且这n个点在正方形上或下边的中点位置 解析: 当然是二分,但建图就有点还行..比较难想..行吧...我太垃圾... 2 - s ...

随机推荐

  1. ·数据库基本内容回顾-day16.06.30

    一. 模式的定义和删除  ---创建了一个模式,就创建了一个数据库命名空间,一个框架.cascade.restrict create schema<模式名> authorization & ...

  2. Duplicate Protocol Definition of DTService Is Ignored

    1.  很多的情况是由于重复导入Protocol导致的. 例如:import "Protocol1.h" import "Protocol1.h" 在同一项目中 ...

  3. 判断两条直线的位置关系 POJ 1269 Intersecting Lines

    两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...

  4. C# SqlHelper

    操作数据库时,经常会把常用的方法封装到一个类中,这里简单写了一个SQLHelper类,供我平时调用. public static class SqlHelper { private static re ...

  5. IO-序列化 Serializable Parcelable Object

    简介 1.什么是序列化和反序列化 对象的寿命通常随着生成该对象的程序的终止而终止,有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复.我们把对象的这种,能记录自己的状态以便将来再生的能力,叫作 ...

  6. 数据库 SQL 外键约束 多表查询

    多表设计与多表查询 1.外键约束        表是用来保存现实生活中的数据的,而现实生活中数据和数据之间往往具有一定的关系,我们在使用表来存储数据时,可以明确的声明表和表之前的依赖关系,命令数据库来 ...

  7. asp.net设置元素css的属性

    controls.style.Add("css名称","css值") 添加class规则 control.cssclass="str_cssname& ...

  8. 国内使用google地图的初级使用

    <!DOCTYPE html><html><head><title>Simple Map</title><meta name=&quo ...

  9. 看android的书的体会

    android书上面的代码有时候有问题,可以在网上搜索这些功能.网上和官方文档里面有很好的说明和例子.

  10. HTML <center> 标签

    浏览器支持 定义和用法所有浏览器都支持 <center> 标签. 对其所包括的文本进行水平居中. HTML 与 XHTML 之间的差异 在 HTML 4.01 中,center 元素不被赞 ...