Go Deeper

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 309    Accepted Submission(s): 122


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 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
The 2010 ACM-ICPC Asia Chengdu Regional Contest
 

Recommend
zhouzeyong
 
 
#include<stdio.h>
#include
<string.h>
#define MAXN 405
#define MAXM 160010
struct Node
{
int from,to,next;
}edge1[MAXM],edge2[MAXM];
struct Node1
{
int a,b,c;
}s[MAXM];
int n,m,head1[MAXN],head2[MAXN],visit1[MAXN],visit2[MAXN],tol1,tol2;
int Tcnt,Bcnt,Belong[MAXN],T[MAXN];
void add(int a,int b)
{
edge1[tol1].from
=a;edge1[tol1].to=b;edge1[tol1].next=head1[a];head1[a]=tol1++;
edge2[tol2].from
=b;edge2[tol2].to=a;edge2[tol2].next=head2[b];head2[b]=tol2++;
}
void dfs1(int i)
{
int j,u;
visit1[i]
=1;
for(j=head1[i];j!=-1;j=edge1[j].next)
{
u
=edge1[j].to;
if(!visit1[u]) dfs1(u);
}
T[Tcnt
++]=i;
}
void dfs2(int i)
{
int j,u;
visit2[i]
=1;
Belong[i]
=Bcnt;
for(j=head2[i];j!=-1;j=edge2[j].next)
{
u
=edge2[j].to;
if(!visit2[u]) dfs2(u);
}
}
int main()
{
int i,ans,right,left,mid,ncase;
scanf(
"%d",&ncase);
while(ncase--)
{
scanf(
"%d%d",&n,&m);
for(i=0;i<m;i++)
scanf(
"%d%d%d",&s[i].a,&s[i].b,&s[i].c);
left
=0;
right
=m;
while(left<=right)
{
mid
=(left+right)/2;
for(i=0;i<2*n;i++)
{
head1[i]
=-1;
head2[i]
=-1;
visit1[i]
=0;
visit2[i]
=0;
}
tol1
=tol2=0;
Tcnt
=Bcnt=0;
for(i=0;i<mid;i++)
{
if(s[i].c==0)
{
add(s[i].a,s[i].b
+n);
add(s[i].b,s[i].a
+n);
}
else if(s[i].c==1)
{
add(s[i].a,s[i].b);
add(s[i].b,s[i].a);
add(s[i].a
+n,s[i].b+n);
add(s[i].b
+n,s[i].a+n);
}
else if(s[i].c==2)
{
add(s[i].a
+n,s[i].b);
add(s[i].b
+n,s[i].a);
}
}
for(i=0;i<2*n;i++)
if(!visit1[i]) dfs1(i);
for(i=Tcnt-1;i>=0;i--)
{
if(!visit2[T[i]])
{
dfs2(T[i]);
Bcnt
++;
}
}
for(i=0;i<n;i++)
{
if(Belong[i]==Belong[i+n]) break;
}
if(i==n)
{
ans
=mid;left=mid+1;
}
else right=mid-1;
}
printf(
"%d\n",ans);
}
return 0;
}


文章来源: http://www.cnblogs.com/kuangbin/archive/2011/08/22/2150058.html