URAL1018 Binary Apple Tree

Source: Internet
Author: User

Very Classic Tree DP (if not the tree DP is recommended first look at the following: I just read the other question first, shake hands or something.

Give a tree, leave n the edge (if it leaves the father's Bembie left), and ask the last Apple how many ....

I find it hard to make a contribution,,, after all, not very much. DP better understand,,,,,,, is to think of the,,,,, as a template problem to record it ~ ~ ~


#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>

using namespace Std;
const int maxn=101,maxq=100;
int N;//jieidanshu
int Q;//baoliudebian
int G[maxn][maxn];//juzhenbiaoshiliangdianguanxi
int son[maxn][2]; Zijiedian
int APPLE[MAXN]; Jiluzijieidanshang de Pingguoshu
int DP[MAXN][MAXN]; Jiyihua
int FLAG[MAXN]; Biaoji
int ISLEAF[MAXN]; Jilushifouziye

void Create (int root)
{
int i,ct=0;
for (i=1;i<=n;i++)
{
if (g[root][i]!=-1&&flag[i]==0)//zhaodaoleziye
{
son[root][ct++] = i; Diyigehuozhediergeziye
Apple[i]=g[root][i];
Flag[i] = 1;
Create (i); Jixujianshu
}
}
if (ct==0)//meiyouziye
{
Isleaf[root]=1;
}
}

int TREEDP (int root, int num)//num Biaoshi shengxiade Dianshu
{
if (num==0) return 0; Bushengxia
if (isleaf[root]==1) return apple[root]; Zuihouyihang
if (dp[root][num]!=-1) return dp[root][num]; Jiyihua
int i,temp=0;
for (i=0;i<num;i++)
{
Temp=max (TEMP,TREEDP (son[root][0],i) +TREEDP (son[root][1],num-1-i));
}
Dp[root][num]=temp+apple[root];
printf ("%d**\n", Dp[root][num]);
return Dp[root][num];
}

int main ()
{
int i,x,y,w;
memset (G,-1,sizeof (g));
scanf ("%d%d", &n,&q);
for (i=1;i<=n-1;i++)
{
scanf ("%d%d%d", &x,&y,&w);
g[x][y]=w;//zhelihenzhongyaodexiangfa!
G[y][x]=w;
}
memset (flag,0,sizeof (flag));
memset (isleaf,0,sizeof (isleaf));
flag[1]=1;//1 Shi Morende
apple[1]=0;
Create (1);
Memset (Dp,-1,sizeof (DP));
printf ("%d\n", TREEDP (1,q+1));
return 0;
}


URAL1018 Binary Apple Tree

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.