bzoj1040 Knight
The main topic: Give each knight's attack and hate people, each knight can not be with the people they hate to appear together, choose some knights make their attack the most.
Idea: Because each knight hates a person, so is a ring set of Senrin (note N-point n-side of the Unicom map may be a ring set of tree forest!!!) ), although the topic is one-way edges, but is equivalent to two-way edges. For the ring-set tree DP practice, you can search the ring, the ring from a place to disconnect, for this problem, the two points after the break is the root of the tree DP (requires root can not be selected), add to the answer. However, we should pay attention to the situation of the two-yuan ring, we can only add one time to the two-yuan ring without the edge (in fact, two have a forward edge), because the two-yuan ring can be treated as a two-point connection, this time there may be a tree without a ring, we need to return to the point of Dfs at the
Must pay great attention to the situation of two Yuan ring!!!
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineLL Long Long#defineMaxnode 1000005using namespacestd;intpoint[maxnode]={0},next[maxnode*2]={0},en[maxnode*2]={0},tot=0, x=0, y=0, ai[maxnode]={0}; LL Val[maxnode]={0},f[maxnode][2]={0},ans=0;BOOLvisit[maxnode]={false},ff;voidAddintUintv) { ++tot;next[tot]=point[u];p oint[u]=tot;en[tot]=v; ++TOT;NEXT[TOT]=POINT[V];p oint[v]=tot;en[tot]=u;}voiddpintUintFA) { inti,j; f[u][1]=val[u];visit[u]=true; for(i=point[u];i;i=Next[i]) { if(en[i]!=FA) { if((u==y&&en[i]==x) | | (u==x&&en[i]==y))Continue; DP (EN[I],U); f[u][0]+=max (f[en[i]][0],f[en[i]][1]); f[u][1]+=f[en[i]][0]; } }}voidDfsintUintFA) { intI,j; LL ci=0; Visit[u]=true; for(i=point[u];i;i=Next[i]) { if(en[i]!=FA) { if(Visit[en[i]]) {x=u;y=En[i]; Memset (F,0,sizeof(f)); DP (x, y); CI=max (ci,f[x][0]); Memset (F,0,sizeof(f)); Swap (x, y);dp(x, y); CI=max (ci,f[x][0]); Ans+=ci;ff=true;return; } Else{DFS (en[i],u);if(FF)return; } } } if(fa==0) {x=y=0;dp(u,0); Ans+=max (f[u][0],f[u][1]); }}intMain () {intn,i,j; scanf ("%d",&N); for(i=1; i<=n;++i) scanf ("%i64d%d",&val[i],&Ai[i]); for(i=1; i<=n;++i)if(ai[ai[i]]!=i| | Ai[i]>i) Add (I,ai[i]); for(i=1; i<=n;++i)if(!visit[i]) {ff=false;d FS (i,0);} printf ("%i64d\n", ans);}View Code
Ring Set Tree