URAL-1018 Binary Apple Tree---treeview dp

Source: Internet
Author: User

Topic Links:

https://cn.vjudge.net/problem/URAL-1018

Main topic:

Give you a tree, each side has a side right, to 1 as the root node, Q bar side of the number of children (q+1 points), edge weights and maximum.

Problem Solving Ideas:

DP[ROOT][J], which represents the root node, preserving the maximum edge and the J nodes.

Dp[root][j]=max (Dp[root][j],dp[root][j-t]+dp[son][t]+len);

T ranges from 1 to j-1, since each point is updated from dp[][1]

1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = -+Ten;4typedefLong Longll;5 structnode6 {7     intV, W;8 node () {}9NodeintVintW): V (v), W (w) {}Ten }; OneVector<node>MAP[MAXN]; A intNUM[MAXN];//Num[i] Indicates the number of points in a subtree with the I node as root - intDP[MAXN][MAXN];//Dp[i][j] indicates that only J-edge maximum weights are in subtrees with the I node as root - voidDfsintRootintFA) the { -Num[root] =1; -      for(inti =0; I < map[root].size (); i++) -     { +         intv = map[root][i].v, w =MAP[ROOT][I].W; -         if(v = = FA)Continue;//No backtracking +Dfs (v, root);//Update your son's information first. ANum[root] + = Num[v];//current number of nodes in root subtree at          for(intj = Num[root]; J >=1; j--)//update the DP of the parent node -         { -              for(intK =1; K < J && K <= Num[v]; k++)//K cannot be equal to j,k=j when the number of root points is 0 -DP[ROOT][J] = max (Dp[root][j], dp[root][j-k] + dp[v][k] +W); -         } -     } in } - intMain () to { +     intN, K; -      while(SCANF ("%d%d", &n, &k)! =EOF) the     { *Memset (DP,0,sizeof(DP)); $          for(inti =1; I < n; i++)Panax Notoginseng         { -             intu, V, W; thescanf"%d%d%d", &u, &v, &W); + map[u].push_back (Node (v, W)); A Map[v].push_back (Node (u, W)); the         } +Dfs1, -1); -cout<<dp[1][k +1]<<endl;//contains K-bars, or k+1 points $     } $     return 0; -}

URAL-1018 Binary Apple Tree---treeview dp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.