Test instructions: There is an n-point m-side of the direction of the graph, each point has its own weight, can be negative, now from a point to go, until you can not go to other points, each through a point, you may choose to get or not to get its weight, each point can go multiple times, but the right value can only be obtained once, Ask how many weights you can get at the end.
Each point can go more than once, the weight can only be obtained once, passing the time of the weight may not be obtained, so we just need to consider the right value on the line. We can get the weights of the points in a strong connected component, so we get the first one with a forward loop, and then we start with DFS at the point where each entry is 0, and the cumulative maximum weight will be the line.
1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 Const intmaxn=3e4+5;8 Const intmaxm=2e5+5;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],ID[MAXN],V[MAXN]; - intDP[MAXN]; -stack<int>S; the - intMaxintAintb) {returnA>b?a:b;} - - voidinit () { +memset (head,-1,sizeof(head)); -size[0]=size[1]=0; +memset (NUM,0,sizeof(num)); Amemset (ID,0,sizeof(ID)); atmemset (dp,-1,sizeof(DP)); - } - - voidAddintAintBintC=0){ -point[c][size[c]]=b; -nxt[c][size[c]]=Head[c][a]; inhead[c][a]=size[c]++; - } to + voidDfsints) { -stx[s]=low[s]=++T; the S.push (S); * for(inti=head[0][s];~i;i=nxt[0][i]) { $ intj=point[0][i];Panax Notoginseng if(!Stx[j]) { - Dfs (j); thelow[s]=min (low[s],low[j]); + } A Else if(!Scc[j]) { thelow[s]=min (low[s],stx[j]); + } - } $ if(low[s]==Stx[s]) { $scccnt++; - while(1){ - intu=S.top (); S.pop (); thescc[u]=scccnt; -Num[scccnt]+=v[u]>0? V[u]:0;Wuyi if(S==u) Break; the } - } Wu } - About voidSETSCC () { $memset (STX,0,sizeof(STX)); -memset (SCC,0,sizeof(SCC)); -T=scccnt=0; - for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); A for(intI=1; i<=n;++i) { + for(intj=head[0][i];~j;j=nxt[0][j]) { the intk=point[0][j]; - if(scc[i]!=Scc[k]) { $Add (Scc[i],scc[k],1); theid[scc[k]]++; the } the } the } - } in the intDFS1 (ints) { the if(~dp[s])returnDp[s]; About intmaxx=0; the for(inti=head[1][s];~i;i=nxt[1][i]) { the intj=point[1][i]; themaxx=Max (MAXX,DFS1 (j)); + } -dp[s]=maxx+Num[s]; the returnDp[s];Bayi } the the intMain () { - intm; - while(SCANF ("%d%d", &n,&m)! =EOF) { the init (); the for(intI=1; i<=n;++i) scanf ("%d",&v[i]); the while(m--){ the intb; -scanf"%d%d",&a,&b); thea++; theb++; the Add (A, b);94 } the SETSCC (); the intans=0; the for(intI=1; i<=scccnt;++i) {98 if(!id[i]) ans=Max (ANS,DFS1 (i)); About } -printf"%d\n", ans);101 }102 return 0;103}
View Code
POJ3160 strong connectivity + Memory search