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[N], b[N], c[N];
int h[N], p[M], v[M], x[N], y[N], cnt = 0;
int dfn[N], low[N], belong[N], ts = 0, top = 0, scc = 0;
int stack[N], instack[N];
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
3
2 1
0 1 0
2 1
0 0 0
2 2
0 1 0
1 1 2
Sample Output
1
1
2