Go Deeper
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2079 Accepted Submission(s): 685
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
Author
CAO, Peng
Source
2010 Asia Chengdu Regional Contest
题意:要是x数组的两个元素相加不能等于c数组的最大元素个数。。。a,b数组表示在x数组中的位置..
2-SAT..建图还是很好建的。。c数组只有3个值0,1,2,用a表示0,a'表示1.。.。x数组只有0和1,a,b数组代表在x数组中的位置,下面a,b表示的值其实都是x数组的值并不是a,b数组的。
c==0时,a-b',b-a'。。表示a取了0,b就不能取0,只能取1.
c==1时,a-b,b-a,a'-b',b'-a',表示a取了0,另一个也只能取0,取1同理。。
c==2时,a'-b,b'-a,表示a取了1,b就不能取1,只能取0.。
然后二分用多少组a,b,c就可以了。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
const int MAXN=410;
const int MAXE=40010;
int head[MAXN],size;
int a[10010],b[10010],c[10010],n,m;
struct EDGE
{
int v,next;
}edge[MAXE];
int sccno[MAXN],low[MAXN],pre[MAXN],dfs_clock,sc_cnt;
stack<int> S;
void init()
{
memset(head,-1,sizeof(head));
memset(sccno,0,sizeof(sccno));
memset(pre,0,sizeof(pre));
dfs_clock=sc_cnt=size=0;
}
void add_edge(int u,int v)
{
edge[size].v=v;
edge[size].next=head[u];
head[u]=size++;
}
void tarjan(int u)
{
pre[u]=low[u]=++dfs_clock;
S.push(u);
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!pre[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!sccno[v])
low[u]=min(low[u],pre[v]);
}
if(pre[u]==low[u])
{
sc_cnt++;
while(1)
{
int x=S.top();
S.pop();
sccno[x]=sc_cnt;
if(u==x)
break;
}
}
}
void build(int mid)
{
init();
for(int i=0;i<mid;i++)
{
if(c[i]==0)
{
add_edge(a[i]<<1,b[i]<<1|1);
add_edge(b[i]<<1,a[i]<<1|1);
}
else if(c[i]==1)
{
add_edge(a[i]<<1,b[i]<<1);
add_edge(b[i]<<1,a[i]<<1);
add_edge(a[i]<<1|1,b[i]<<1|1);
add_edge(b[i]<<1|1,a[i]<<1|1);
}
else
{
add_edge(a[i]<<1|1,b[i]<<1);
add_edge(b[i]<<1|1,a[i]<<1);
}
}
}
bool test()
{
int i;
for(i=0;i<2*n;i++)
if(!pre[i])
tarjan(i);
for(i=0;i<n;i++)
if(sccno[i<<1]==sccno[i<<1|1])
return 0;
return 1;
}
int main()
{
int t,i,j;
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]);
}
int l=0,r=m;
int ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
build(mid);
if(test())
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}