Title Link: http://61.187.179.132/JudgeOnline/problem.php?id=1040
Test instructions: Give a graph with only one ring. Each point has a weighted value. Select some points 22 is not adjacent, so that the weight of the largest?
There is only one ring, then we first build, then there is only one non-tree edge, non-tree edge on the two points we set as node and root, then we DP on both sides, first let node can not select, and then let the root can not be selected, DP two times can, and the ring edge can not go.
1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6 #definell Long Long7 inttot,go[2000005],next[2000005],first[1000005],b[2000005];8 intvis[1000005],root,tmp,node,flag,n;9ll f[1000005][2],w[1000005];Ten voidInsertintXintYintID) { Onetot++; Ago[tot]=y; -next[tot]=First[x]; -first[x]=tot; theb[tot]=ID; - } - voidAddintXintYintID) { - Insert (x,y,id); + Insert (y,x,id); - } + voidDfsintXintFA) { Avis[x]=1; at for(intI=first[x];i;i=Next[i]) { - intPur=Go[i]; - if(PUR==FA)Continue; - if(Vis[pur]) { -root=pur; -Node=x; intmp=B[i]; - Continue; to } + DFS (pur,x); - } the } * voiddpintXintFA) { $f[x][0]=0; f[x][1]=0;Panax Notoginseng for(intI=first[x];i;i=Next[i]) { - intPur=Go[i]; the if(b[i]==tmp| | PUR==FA)Continue; + DP (PUR,X); Af[x][0]+=std::max (f[pur][0],f[pur][1]); thef[x][1]+=f[pur][0]; + } -f[x][1]+=W[x]; $ if(flag==0&&x==root) f[x][1]=0; $ if(flag==1&&x==node) f[x][1]=0; - } - intMain () { thescanf"%d",&n); - intId=0;Wuyi for(intI=1; i<=n;i++){ the intx; -scanf"%lld%d",&w[i],&x); Wuid++; - Add (i,x,id); About } $ll ans=0; -ll ans=0; - for(intI=1; i<=n;i++) - if(!Vis[i]) { Anode=tmp=0; +DFS (I,0); theflag=1; -DP (Root,0); $Ans=std::max (f[root][0],f[root][1]); theflag=0; theDP (Root,0); theAns=std::max (Ans,std::max (f[root][0],f[root][1])); theans+=Ans; - } inprintf"%lld", ans); the}
Bzoj 1040 Knight