zoj3422Go Deeper(2-sat + 二分)
来源:互联网 发布:帝陀 知乎 编辑:程序博客网 时间:2024/04/29 04:48
题目请戳这里
题目大意:
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 + 二分)
- 二分+2SAT zoj3422 Go Deeper
- hdu3715 Go Deeper--二分 & 2-sat
- HDU 3715 Go Deeper 二分 + 2-sat
- HDU 3715 Go Deeper【2-SAT+二分】
- HDOJ 3715 Go Deeper (2Sat: 二分答案判定)
- hdu 3715 Go Deeper(二分+2-sat判定)
- HDU 3715 Go Deeper(2-SAT + 二分判定)
- HDU 3715 Go Deeper(2-SAT + 二分)
- HDOJ 3715 - Go Deeper 二分+2-sat判断
- zoj 3422 Go Deeper ( 二分+2-sat )
- hdoj 3715 Go Deeper 【2-sat 判断可行解 + 二分】
- HDU - 3715 Go Deeper (二分 + 2-SAT)
- HDU 3715 Go Deeper 2-SAT 二分答案
- Poj 2723 Go Deeper【2-SAT-----Tarjan强连通+二分】
- HDU 3715 Go Deeper(二分+2-SAT总结)
- 2-sat->HDU 3715 Go Deeper
- HDU 3715 Go Deeper(2-SAT)
- POSIX 线程详解 一种支持内存共享的简捷工具
- 通用线程:POSIX 线程详解
- HTML5迷你游戏作验证码
- C++中sort与quicksort调用方法
- 通用线程:POSIX 线程详解
- zoj3422Go Deeper(2-sat + 二分)
- hibernate 1+n问题
- hoj1249-三角形
- Linux 线程实现机制分析
- Java赋值与构造
- 深入浅出Spring(五) SpringMVC
- Git
- Django中生成PDF(一)
- 独立游戏大卖背后的制作者辛酸历程