[Ural 1018] binary apple tree [tree DP]

Source: Internet
Author: User

Question:

There is an apple tree. The root node number is 1. The edge weight is the number of apples on the edge. the maximum number of apples that can be retained when x branches are retained.

Ideas:

Tree DP.

The DP [I] [J] indicates the maximum number of apples that a subtree with I as the root retains.

To enumerate different sub-tree allocation schemes, select the largest one.

The special feature of the tree DP is that its first-dimensional change follows the DFS direction.

# Include <cstdio> # include <cstring> const int maxn = 111; int N, M, F [maxn] [maxn], data [maxn] [maxn]; bool vis [maxn]; struct tree {int L, R, V; inline tree () {L = r = V = 0 ;}} A [maxn]; /// A is a forest... inline void build (const Int & X) // recursion can also be inline... in this way, the stack will not pop up? {// Const reference, so a constant can also be passed in. In order to save memory? Vis [x] = true; For (INT I = 1; I <= N; I ++) if (! Vis [I] & Data [x] [I]) {If (! A [X]. l) A [X]. L = I; else // It is a binary A [X]. R = I; A [I]. V = data [x] [I]; // drops the edge weight to the remote point right build (I) ;}} inline int DFS (const Int & X, const Int & left) {If (! X |! Left) // If (F [x] [left]) if (F [x] [left]) // returns f [x] [left] by means of memory; /// the subdivides will overlap int maxn = 0; For (INT I = 0; I <left; I ++) /// the number of actually retained items must be smaller than 1 {int L = DFS (A [X]. l, I), r = DFS (A [X]. r, left-I-1); If (maxn <L + r) maxn = L + R;} // you can add yourself to the returned result. in fact, this statement executes the "add" operation. this is reasonable. f [x] [left] = maxn + A [X]. v; // return f [x] [left];} int main () {While (scanf ("% d", & N, & M )! = EOF) {memset (data, 0, sizeof (data); memset (VIS, false, sizeof (VIS); memset (F, 0, sizeof (f )); for (INT I = 1; I <= N; I ++) A [I] = tree (); // assign each element to a (IS THIS SENTENCE redundant ?) For (INT I = 1; I <n; I ++) {int tmp1, tmp2, tmpv; scanf ("% d", & tmp1, & tmp2, & tmpv); Data [tmp1] [tmp2] = data [tmp2] [tmp1] = tmpv; // adjacent matrix} build (1); int ans = DFS (1, m + 1); // leave m branches, but one is connected to its own node. // This is always followed during the DFS process; otherwise, it cannot be split. /// only the edges leading to the son node can be transferred to the son tree. Otherwise, the printf ("% d \ n", ANS) will be chaotic during statistics;} return 0 ;}

Repeat it by yourself:

#include <cstdio>#include <vector>using namespace std;const int MAXN = 105;int dp[MAXN][MAXN],n,m;typedef struct node{    int v,w;    node(){}    node(int _v, int _w):v(_v),w(_w){}}node;typedef struct tree{    int l,r,w;    tree(){}}tree;vector<node> edge[MAXN];tree a[MAXN];bool vis[MAXN];inline void build(const int& x){    vis[x] = true;    for(int i=0;i<edge[x].size();i++)    {        int y = edge[x][i].v;        if(!vis[y])        {            if(!a[x].l)                a[x].l = y;            else                a[x].r = y;            a[y].w = edge[x][i].w;            build(y);        }    }}inline int dfs(const int& i, const int& j){    if(!i || !j)    return 0;    if(dp[i][j])   return dp[i][j];    int max = 0;    for(int k=0;k<j;k++)    {        int l = dfs(a[i].l, k), r = dfs(a[i].r, j-1-k);        if(max<l+r)            max = l+r;    }    dp[i][j] = max + a[i].w;    return dp[i][j];}int main(){    scanf("%d %d",&n,&m);    for(int i=1,u,v,w;i<n;i++)    {        scanf("%d %d %d",&u,&v,&w);        edge[u].push_back(node(v,w));        edge[v].push_back(node(u,w));    }    build(1);    int ans = dfs(1, m+1);    printf("%d\n",ans);}

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.