Test instructions: For a forward graph, ask the maximum number of points in the group, requiring at least one path between all the point pairs in the point set (U to V or V to u or two bars).
First of all, for each strong connected component, where all the points must be able to reach each other, so the first indentation, and then for the dag,dp[i after the indentation of the maximum number of points that can be reached from the I-strong connected component, then at the point of indentation, we need to record the points of each strong connected component. Then the DP, the initial value is the number of points of the strong connected component, and then use the point it can reach to update itself. Take the maximum value on the line.
1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 Const intmaxn=1005;8 Const intmaxm=50005;9 Ten inthead[2][maxn],point[2][maxm],nxt[2][maxm],size[2]; One intn,t,scccnt; A intSTX[MAXN],LOW[MAXN],SCC[MAXN],NUM[MAXN],DP[MAXN]; -stack<int>S; - the voidinit () { -memset (head,-1,sizeof(head)); -size[0]=size[1]=0; -Memset (DP,0,sizeof(DP)); + } - + voidAddintAintBintC=0){ Apoint[c][size[c]]=b; atnxt[c][size[c]]=Head[c][a]; -head[c][a]=size[c]++; - } - - voidDfsints) { -stx[s]=low[s]=++T; in S.push (S); - for(inti=head[0][s];~i;i=nxt[0][i]) { to intj=point[0][i]; + if(!Stx[j]) { - Dfs (j); thelow[s]=min (low[s],low[j]); * } $ Else if(!Scc[j]) {Panax Notoginsenglow[s]=min (low[s],stx[j]); - } the } + if(low[s]==Stx[s]) { Ascccnt++; the while(1){ + intu=S.top (); S.pop (); -scc[u]=scccnt; $num[scccnt]++; $ if(S==u) Break; - } - } the } - Wuyi voidSETSCC () { thememset (STX,0,sizeof(STX)); -memset (SCC,0,sizeof(SCC)); Wumemset (NUM,0,sizeof(num)); -T=scccnt=0; About for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); $ for(intI=1; i<=n;++i) { - for(intj=head[0][i];~j;j=nxt[0][j]) { - intk=point[0][j]; - if(scc[i]!=Scc[k]) { AAdd (Scc[i],scc[k],1); + } the } - } $ } the the voidDFS1 (ints) { the if(Dp[s])return; thedp[s]=Num[s]; - for(inti=head[1][s];~i;i=nxt[1][i]) { in intj=point[1][i]; the Dfs (j); the if(Num[s]+dp[j]>dp[s]) dp[s]=num[s]+Dp[j]; About } the } the the intMain () { + intT; -scanf"%d",&T); the while(t--){Bayi intm; thescanf"%d%d",&n,&m); the init (); - while(m--){ - intb; thescanf"%d%d",&a,&b); the Add (A, b); the } the SETSCC (); - intans=0; the for(intI=1; i<=scccnt;++i) { the DFS1 (i); the if(Ans<dp[i]) ans=Dp[i];94 } theprintf"%d\n", ans); the } the return 0;98}
View Code
UVA11324 Strong connected +DP memory search