HDU 3715 Go Deeper 2-SAT 二分答案
2016-03-25 22:56
483 查看
if dep<m and x[a[dep]]+x[b[dep]]!=c[dep] then go(dep+1,n,m)
意思就是说,对于
dep=0~m-1,有所有的不等式都成立。
而
c[dep]只有3种取值
取0时,x[a[dep]]和x[b[dep]]不同时为0,即x[a[dep]] or x[b[dep]] != 0
取1时,x[a[dep]]和x[b[dep]]不为0和1或1和0,即x[a[dep]] xor x[b[dep]] != 1
取2时,x[a[dep]]和x[b[dep]]不同时为1,即x[a[dep]] and x[b[dep]] != 1
于是2-sat二分dep
#include <cstdio> #include <memory.h> #include <cmath> #include <algorithm> using namespace std; #define rep(i,j,k) for(i=j;i<k;++i) #define sqr(i) ((i)*(i)) #define ms(i) memset(i,0,sizeof(i)) const int N = 20010, M = 600000; int a , b , c ; int h , p[M], v[M], x , y , cnt = 0; int dfn , low , belong , ts = 0, top = 0, scc = 0; int stack , instack ; void add(int a, int b) { p[++cnt] = h[a]; v[cnt] = b; h[a] = cnt; } void tarjan(int x) { int i; dfn[x] = low[x] = ++ts; instack[x] = 1; stack[++top] = x; for (i = h[x]; i; i = p[i]) if (!dfn[v[i]]) { tarjan(v[i]); low[x] = min(low[v[i]], low[x]); } else if (instack[v[i]]) low[x] = min(dfn[v[i]], low[x]); if (dfn[x] == low[x]) { ++scc; do { i = stack[top--]; instack[i] = 0; belong[i] = scc; } while (i != x); } } int main() { #define id(i,j) (2*(i)+(j)) int n, m, i, j, ans, l, r, mid, t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); rep(i,0,m) scanf("%d%d%d", a+i,b+i,c+i); l = 0, r = m; while (l <= r) { mid = l + r >> 1; ms(dfn); ms(h); cnt = 0; ts = 0; scc = 0; rep(i,0,mid) switch (c[i]) { case 0: add(id(a[i],0),id(b[i],1)); add(id(b[i],0),id(a[i],1)); break; case 1: add(id(a[i],0),id(b[i],0)); add(id(a[i],1),id(b[i],1)); add(id(b[i],0),id(a[i],0)); add(id(b[i],1),id(a[i],1)); break; case 2: add(id(a[i],1),id(b[i],0)); add(id(b[i],1),id(a[i],0)); break; } rep(i,0,2*mid) if (!dfn[i]) tarjan(i); bool flag = true; rep(i,0,mid) if (belong[id(i,0)] == belong[id(i,1)]) { flag = false; break; } if (flag) l = mid + 1, ans = mid; else r = mid - 1; } printf("%d\n", ans); } return 0; }
Go Deeper
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2756 Accepted Submission(s): 869
Problem Description
Here is a procedure’s pseudocode: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
In this code n is an integer. a, b, c and x are 4 arrays of integers. The index of array always starts from 0. Array a and b consist of non-negative integers smaller than n. Array x consists of only 0 and 1. Array c consists of only 0, 1 and 2. The lengths of array a, b and c are m while the length of array x is n. Given the elements of array a, b, and c, when we call the procedure go(0, n, m) what is the maximal possible value the procedure may output?
Input
There are multiple test cases. The first line of input is an integer T (0 < T ≤ 100), indicating the number of test cases. Then T test cases follow. Each case starts with a line of 2 integers n and m (0 < n ≤ 200, 0 < m ≤ 10000). Then m lines of 3 integers follow. The i-th(1 ≤ i ≤ m) line of them are ai-1 ,bi-1 and ci-1 (0 ≤ ai-1, bi-1 < n, 0 ≤ ci-1 ≤ 2).Output
For each test case, output the result in a single line.Sample Input
32 1
0 1 0
2 1
0 0 0
2 2
0 1 0
1 1 2
Sample Output
11
2
相关文章推荐
- Google源码下载,通过清华大学 TUNA 镜像源
- Webgoat学习笔记1
- [django]自定义全局context
- 关于OGR读取多边形-Polygon(GDAL 2.0版本之前)
- django-模型超级总结
- go语言学习心得
- zjnu1726 STOGOVI (lca)
- Django CSRF_TOKEN跨域攻击处理
- POJ 2195 Going Home (最小费用最大流)
- Go语言核心之美 3.4-Struct结构体
- GoDaddy重推CN域名注册服务
- GoDaddy重推CN域名注册服务
- hdu 5645 DZY Loves Balls
- hdu 5646 DZY Loves Partition
- Mastering the game of Go with deep neural networks and tree search 中文版来了
- codeforces_616D. Longest k-Good Segment(尺取法)
- Django-Model中的ForeignKey,ManyToManyField与OneToOneField
- go语言映射map的线程协程安全问题
- Django框架利用ajax实现批量导入数据功能
- 简学LINGO(三)——实例篇