Description
The Knights of Z are a powerful organization that brings together elites from all over the world. They maxi and punish evil and Good, and are praised by all sectors of the community. A terrible thing has happened recently, and the evil Y countries have launched a war of aggression against Z-states. The war stretches for 500 miles, and the Z-nation, who has been comfortably in peace for hundreds of years, can withstand the forces of the nations of Y. So people pinned all their hopes on the Knights, as if expecting the birth of a true dragon, leading Justice to defeat evil. The Knights were certainly capable of defeating the forces of evil, but the Knights often had some contradictions with each other. Every knight has and only one of his own most hated Knight (not himself), he is absolutely not with his most dislike of the people to the expedition together. War stretches, the people misery, organized a Knight Corps to join the Battle of urgency! The king has given you a formidable task, electing a Knight Corps from all the knights, so that there are no contradictory members of the Legion (there is no knight in the case of the Knights with whom he hates the most), and that the Knight Corps is the most capable of fighting. In order to describe the combat effectiveness, we numbered the Knights according to 1 to N, giving each knight an estimate of the combat effectiveness of a legion that is the sum of the fighting power of all knights.
Input
The first line contains a positive integer n, which describes the number of knights. The next n lines, two positive integers per line, describe in order the combat effectiveness of each knight and the knight he hates most.
Output
Should contain a line containing an integer representing the combat effectiveness of your chosen Knight Corps.
Sample Input3
10 2
20 3
1Sample Output -HINT
For 100% of the test data, meet N≤1 000 000, each knight's combat effectiveness is not greater than 1 000 000 positive integer.
Base Ring tree dp, remove the ring, record the split two points, force two points in a point for the root, and do not choose to run DP, and then force another point for the root does not choose to run DP, take the big encounter a problem, is the ring by the two composition how to do, my solution is to make them do not ring direct run DP, the
1#include <cstdio>2#include <iostream>3#include <cstring>4 #definell Long Long5 using namespacestd;6 Const intn=1000100;7 intQ[n],du[n],v[n],c[n],deep[n],fa[n],head[n],sz[n];8 intRoot,other,cnt,n,tot;9ll ans,end,tr[n][2];Ten BOOLVis[n],pd[n],viss[n]; One structee{intTo,next;} e[n*2]; A voidInsintUintv) { -e[++cnt].next=head[u],e[cnt].to=v,head[u]=cnt;du[u]++; -e[++cnt].next=head[v],e[cnt].to=u,head[v]=cnt;du[v]++; the } - voidTopsort () { - intL,r;l=r=0; - for(intI=1; i<=n;i++)if(du[i]==1) q[++r]=i,vis[i]=1; + while(l<R) { - intnow=q[++l]; + for(intI=head[now];i;i=E[i].next) { A intv=e[i].to; at if(du[v]>1){ -du[v]--; - if(du[v]==1) q[++r]=v,vis[v]=1; - } - } - } in } - to voidDfsintXintt) { +vis[x]=t;c[x]=T; - for(intI=head[x];i;i=E[i].next) { the intv=e[i].to; * if(!Vis[v]) DFS (v,t); $ }Panax Notoginseng } - the +ll TREEDP (intXintBzintt) { A if(tr[x][bz]!=-1)returnTR[X][BZ]; thell mid=0;BOOLflag=0; viss[x]=1; + for(intI=head[x];i;i=E[i].next) { - intvv=e[i].to; $ if(deep[vv]==deep[x]+1) { $flag=1; - if(bz==0) Mid+=max (TREEDP (vv,bz^1, T), TREEDP (vv,bz,t)); - ElseMID=MID+TREEDP (vv,bz^1, T); the } - }Wuyi if(bz==1) mid+=V[x]; the if(!flag) tr[x][bz]=bz*v[x];Elsetr[x][bz]=mid; - returnTR[X][BZ]; Wu } - About voidSolveintXintBzintt) { $vis[x]=1; - for(intI=head[x];i;i=E[i].next) { - intv=e[i].to; - if(X==root&&v==other)Continue; A if(!vis[v]) deep[v]=deep[x]+1, Solve (v,bz,t); + } the } - $ intMain () { thescanf"%d", &n);intx; the for(intI=1; i<=n;i++){ thescanf"%d%d",&v[i],&x); thesz[i]=x; - in if(sz[x]!=i) Ins (i,x); thefa[x]=i; the } About Topsort (); the for(intI=1; i<=n;i++)if(!vis[i]) DFS (i,++tot); thememset (tr,-1,sizeof(tr)); the for(intI=1; i<=n;i++){ + if(c[i]&&!Pd[c[i]]) { -Other=0; root=i;deep[root]=0;PD [c[i]]=1; the for(intj=head[i];j;j=E[j].next) {Bayi if(c[e[j].to]==C[i]) { theOther=e[j].to; the Break; - } -}ans=0; thememset (Vis,0,sizeof(VIS)); vis[root]=1; theSolve (Root,0, C[i]); theAns=max (ANS,TREEDP (Root,0, C[i])); theroot=other;deep[root]=0; -Other=i; thememset (Vis,0,sizeof(VIS)); vis[root]=1; thememset (tr,-1,sizeof(tr)); theSolve (Root,0, C[i]);94Ans=max (ANS,TREEDP (Root,0, C[i])); theend+=ans; the } the }98other=-1; Aboutmemset (Vis,0,sizeof(Vis)); - for(intI=1; i<=n;i++)if(!Viss[i]) {101Root=i;solve (Root,0, C[i]);102End+=max (TREEDP (Root,0,0), TREEDP (Root,1,0));}103printf"%lld", end);104}
"Bzoj 1040" [ZJOI2008] Knight