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 descriptionhere is a procedure's pseudo code:
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?
Inputthere 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 ).
Outputfor each test case, output the result in a single line.
Sample Input
32 10 1 02 10 0 02 20 1 01 1 2
Sample output
112
Authorcao, Peng
Source2010 Asia Chengdu Regional Contest
Recommendzhouzeyong question: http://acm.hdu.edu.cn/showproblem.php? PID = 3715 question: Let's count M groups A, B, and C. X [a] + X [B]! = The maximum number of consecutive groups of C, X is an array of N length, the value is unknown, 0 or 1 Analysis: we can assume that all the values of the X array, then several sub-statements are determined in sequence, and then 2-Sat is taken into consideration... I thought about it in turn. If the first I sub-statement is set up, you can use 2-sat to determine whether the pre-I array can be set up. Because m is large, add a binary key to the OK code:
#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;}