Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5293
Test instructions: Give you a tree of n points, and some tree chain on the tree, each tree chain has a certain weight, asked to select some disjoint tree chain, so that their weights and as large as possible
Solution:
Tree Dp,dp[x] Represents the solution of a subtree with x as the root node, then the DP can be based on the selection and non-selection of the tree chain at the current node.
Now there are two problems:
1, which point on the tree chain to choose?
2, select and do not choose how to operate separately?
For the first question, take a rough look (as you'll find in solving problem two), the lightest point on the tree chain (i.e. LCA at both ends of the tree chain) is feasible.
For the second question, (assuming now at point U), Dp[u]=sum (Dp[v]) (V is a child of U), Record Sum (Dp[v]) with Sum[u] (V is a child of U), with Dp[u]=sum[u]
Choice of words, for each LCA at the U point of the tree chain C,
Dp[u]=max (Dp[u],sum (Dp[v1]) +C.W (V1 is a child node of all nodes on C, C.W is the weight of the chain))
Dp[u]=max (Dp[u],sum (sum[x])-sum (dp[x]) +C.W (x is all nodes on C, C.W is the weight of the chain))
The value of all nodes on this chain and the maintenance of a DFS sequence + tree array is fine.
1 /*2 * Problem:3 * AUTHOR:SHJWUDP4 * Created TIME:2015/8/2 Sunday 22:07:325 * File name:233.cpp6 * State:7 * Memo:8  */9#include <iostream>Ten#include <cstdio> One#include <cstring> A#include <algorithm> -#include <vector> - #pragmaComment (linker, "/stack:1024000000,1024000000") the  - using namespacestd; -  - structEdge { +     intu, v; -Edge (intUintv): U (U), V (v) {} + }; A structChain { at     intA, B, W; -     intLCA; - }; - structFenwick { -     intN; -vector<int>C; in     voidInitintN) { -          This->n=N; toC.assign (n+1,0); +     } -     intLowbit (intx) { the         returnX &-x; *     } $     voidAddintXintv) {Panax Notoginseng          while(x<=N) { -C[x]+=v; x+=lowbit (x); the         } +     } A     intGetsum (intx) { the         intres=0; +          while(x>0) { -RES+=C[X]; x-=lowbit (x); $         } $         returnRes; -     } - } fw1, fw2; the  - intN, M;WuyiVector<edge>edges; thevector<vector<int> >G; -Vector<chain>chain; Wuvector<int>Ln, RN, DEP, DP, sum; -vector<vector<int> >FA, arr; About introot, cnt; $ voidInitintSZ) { - edges.clear (); -G.assign (SZ, vector<int> (0)); - chain.resize (SZ); A ln.resize (SZ); rn.resize (SZ); dep.resize (SZ); dp.resize (SZ); Sum.resize (SZ) ; +Fa.assign (SZ, vector<int> ( -)); theArr.assign (SZ, vector<int> (0)); -Fw1.init (sz<<1); Fw2.init (sz<<1); $ } the voidAddedge (intUintv) { the Edges.push_back (Edge (U, v)); theG[u].push_back (Edges.size ()-1); the } - voidDFS1 (intu) { inln[u]=++CNT; the      for(intk=1; k< -; k++) fa[u][k]=fa[fa[u][k-1]][k-1]; the      for(intI:g[u]) { AboutEdge & e=Edges[i]; the         if(e.v==fa[u][0])Continue; thefa[e.v][0]=u; thedep[e.v]=dep[u]+1; + DFS1 (E.V); -     } thern[u]=++CNT;Bayi } the intLcaintUintv) { the     if(dep[u]<Dep[v]) Swap (U, v); -      for(intk= +; k>=0; k--) { -         if(dep[fa[u][k]]>=Dep[v]) { theu=Fa[u][k]; the         } the     } the     if(U==V)returnu; -      for(intk= +; k>=0; k--) { the         if(fa[u][k]!=Fa[v][k]) { theU=FA[U][K]; v=Fa[v][k]; the         }94     } the     returnfa[u][0]; the } the voidDFS2 (intu) {98dp[u]=sum[u]=0; About      for(intI:g[u]) { -Edge & e=Edges[i];101         if(e.v==fa[u][0])Continue;102 DFS2 (E.V);103sum[u]+=DP[E.V];104     } thedp[u]=Sum[u];106      for(intI:arr[u]) {107Chain & c=Chain[i];108         intTmp=fw1.getsum (LN[C.A]) +fw1.getsum (ln[c.b])109-fw2.getsum (LN[C.A])-fw2.getsum (LN[C.B]) +Sum[u]; theDp[u]=max (Dp[u], tmp+C.W);111     } theFw1.add (Ln[u], sum[u]); Fw1.add (Rn[u],-Sum[u]);113Fw2.add (Ln[u], dp[u]); Fw2.add (Rn[u],-Dp[u]); the } the intMain () { the #ifndef Online_judge117Freopen ("inch","R", stdin);118     //freopen ("Out", "w", stdout);119 #endif -     intT;121scanf"%d", &T);122      while(t--) {123scanf"%d%d", &n, &m);124Init (n+1); the          for(intI=1; i<n; i++) {126             intA, B;127scanf"%d%d", &a, &b); - Addedge (A, b);129 Addedge (b, a); the         }131root=1; Cnt=0; dep[root]=0; fa[root][0]=root; dfs1 (root); the          for(intI=0; i<m; i++) {133scanf"%d%d%d", &CHAIN[I].A, &chain[i].b, &CHAIN[I].W);134Chain[i].lca=LCA (CHAIN[I].A, chain[i].b);135 Arr[chain[i].lca].push_back (i);136         }137 DFS2 (root);138printf"%d\n", Dp[root]);139     } $     return 0;141}HDU 5293
 [2015HDU Multi-School league title]hdu5293 Tree chain problem