"The main topic"
There are N Knights, given their abilities and the most hated knight. Elect a legion of knights, so that there are no contradictions within 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 effective and capable of fighting the maximum.
Ideas
First yy a bit, you can know that this is a base ring forest. We can use the following methods:
First, find an edge (implemented with DFS) on each ring of the base ring tree, remembering that its two endpoints are U and v. Then delete this edge (I'm using this method to record the position of the u,v in the other container and ignore the edge in subsequent operations). Since U and V cannot be taken at the same time, there are two cases where the edge between U and V is deleted:
G[i] is the maximum combat power of I and its subtree without taking I, and f[i] means taking I.
(1) U do not take, v any. The tree DP with u as the root, the result is g[u];
(2) v not taken, u any. The tree DP is the root of V, and the result is g[v].
Then Ans+max (G[u],g[v]).
The process of the tree DP is very routine, so don't dwell on it.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6 using namespacestd;7 Const intmaxn=1000000+ -;8typedefLong Longll;9vector<int>E[MAXN];Ten intN,POWER[MAXN],HATE[MAXN]; One intVIS[MAXN]; A intu,v; - ll G[MAXN],F[MAXN]; - the voidAddedge (intUintv) - { - E[u].push_back (v); - e[v].push_back (u); + } - + voidDfsintUintfr) A { atvis[u]=1; - for(intI=0; I<e[u].size (); i++) - { - intto=E[u][i]; - if(to!=fr) - { in if(!Vis[to]) DFS (to,u); - Else to { +vis[to]=1; -U=u; v=to ; the return; * } $ }Panax Notoginseng } - } the + voidTREEDP (intUintFrintRtintban) A { thevis[u]=1; +f[u]=Power[u]; -g[u]=0; $ for(intI=0; I<e[u].size (); i++) $ { - intto=E[u][i]; - if(U==rt && I==ban)Continue; the if(To!=fr && to!=RT) - {Wuyi TREEDP (To,u,rt,ban); thef[u]+=G[to]; -g[u]+=Max (g[to],f[to]); Wu } - } About } $ - voidInit () - { -scanf"%d",&n); A for(intI=1; i<=n;i++) + { thescanf"%d%d",&power[i],&hate[i]); - Addedge (I,hate[i]); $ } the } the the voidGet_ans () the { -memset (Vis,0,sizeof(Vis)); inll ans=0; the for(intI=1; i<=n;i++) the if(!Vis[i]) About { theDFS (i,-1); the intBanu,banv; the for(intI=0; I<e[u].size (); i++)if(e[u][i]==V) + { -banu=i; the Break;Bayi } the for(intI=0; I<e[v].size (); i++)if(e[v][i]==U) the { -banv=i; - Break; the } the the theTREEDP (u,-1, U,banu); -ll uans=G[u]; the theTREEDP (v,-1, V,BANV); thell vans=G[v];94ans+=Max (Uans,vans); the } thecout<<ans<<Endl; the }98 About intMain () - {101 init ();102 Get_ans ();103 return 0; 104}
"Tree-shaped DP" bzoj1040-[zjoi2008) Knight