Go Deeper
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 988 Accepted Submission(s): 351
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?
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 a
i-1 ,b
i-1 and c
i-1 (0 ≤ a
i-1, b
i-1 < n, 0 ≤ c
i-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
Recommend
zhouzeyong
题意:给你m组数a、b、c,求满足 x[a]+x[b]!=c的最多连续的组数,x为长度n 的数组,值未确定,为0或1
分析:我们可以假设x数组的所有值,然后依次判定有几个式子成立,然后就想到了2-sat。。。
也就反过来想了,假设前i个式子成立,那么可以用2-sat判断,是否能满足前i个数组成立,由于m比较大,加个二分就ok
代码:
#include<cstdio>
#include<iostream>
using namespace std;
const int mm=44444;
const int mn=444;
int ver[mm],next[mm],a[mm],b[mm],c[mm];
int head[mn],dfn[mn],low[mn],q[mn],id[mn];
int i,j,k,n,m,l,r,t,mid,idx,top,cnt,edge,ans;
void add(int u,int v)
{
ver[edge]=v,next[edge]=head[u],head[u]=edge++;
}
void dfs(int u)
{
dfn[u]=low[u]=++idx;
q[top++]=u;
for(int i=head[u],v;i>=0;i=next[i])
if(!dfn[v=ver[i]])
dfs(v),low[u]=min(low[u],low[v]);
else if(!id[v])low[u]=min(low[u],dfn[v]);
if(dfn[u]==low[u])
{
id[u]=++cnt;
while(q[--top]!=u)id[q[top]]=cnt;
}
}
void Tarjan()
{
for(idx=cnt=top=i=0;i<n+n;++i)dfn[i]=id[i]=0;
for(i=0;i<n+n;++i)
if(!dfn[i])dfs(i);
}
bool ok()
{
Tarjan();
for(i=0;i<n+n;i+=2)
if(id[i]==id[i^1])return 0;
return 1;
}
int main()
{
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]);
ans=l=0,r=m;
while(l<=r)
{
mid=(l+r)>>1;
for(edge=i=0;i<n+n;++i)head[i]=-1;
for(i=0;i<mid;++i)
{
if(c[i]==0)add(a[i]<<1,b[i]<<1|1),add(b[i]<<1,a[i]<<1|1);
if(c[i]==1)add(a[i]<<1,b[i]<<1),add(b[i]<<1,a[i]<<1),
add(a[i]<<1|1,b[i]<<1|1),add(b[i]<<1|1,a[i]<<1|1);
if(c[i]==2)add(a[i]<<1|1,b[i]<<1),add(b[i]<<1|1,a[i]<<1);
}
if(ok())ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}