【题目链接】

点击打开链接

【算法】

树形背包

注意是一棵森林

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 210 int i,n,m,tot,x;
int head[MAXN],val[MAXN],f[MAXN][MAXN],size[MAXN]; struct Edge
{
int nxt,to;
} e[MAXN]; inline void add(int x,int y)
{
tot++;
e[tot] = (Edge){head[x],y};
head[x] = tot;
} inline void dfs(int x)
{
int i,j,k,y;
size[x] = ;
for (i = head[x]; i; i = e[i].nxt)
{
y = e[i].to;
dfs(y);
size[x] += size[y];
for (j = size[x]; j >= ; j--)
{
for (k = ; k < j; k++)
{
f[x][j] = max(f[x][j],f[x][k]+f[y][j-k]);
}
}
}
} int main() { while (scanf("%d%d",&n,&m) != EOF)
{
if (!n && !m) break;
tot = ;
memset(f,,sizeof(f));
memset(size,,sizeof(size));
for (i = ; i <= n; i++) head[i] = ;
for (i = ; i <= n; i++)
{
scanf("%d%d",&x,&val[i]);
add(x,i);
f[i][] = val[i];
}
dfs();
printf("%d\n",f[][m+]);
} return ; }

【HDU 1561】 The More,The better的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  8. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

  9. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

随机推荐

  1. VIM键盘图

  2. poj1091:跳蚤【容斥原理】

    题目大意:中文题就不翻译了 思路:假设跳蚤选择X1个第一张卡片,X2个第二张卡片...Xn个第n张卡片,Xn+1张写着m的卡片,那么就可以列出方程:a1*X1+a2*X2+…+an*Xn+m*X(n+ ...

  3. hdu3853:LOOPS

    题目大意:r*c个点,每个点有Aij的概率回到自己本身,Bij的概率向右一格,Cij的概率向下一格,求从(1,1)到(r,c)的期望步数. 题解:有了hdu4405的经验,从后往前推期望.那么,E(i ...

  4. OC-scrollview加载多个控制器界面的优化

    在开发过程中,经常有一个控制器中多个字控制器界面的管理,如下图: 这种实现方式,很多种,今天主要记录用scrollview实现的方法.并且只加载当前显示界面的数据. 思路: (1)创建3个需要展示的控 ...

  5. Bootstrap3 为何无法显示Glyphicons 图标

    Bootstrap3 为何无法显示Glyphicons 图标 在CSS引入字体即可解决 @font-face { font-family: 'Glyphicons Halflings'; src: u ...

  6. PAT (Advanced Level) 1036. Boys vs Girls (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  7. CodeForces 599C Day at the Beach

    预处理一下i到n的最小值. #include<cstdio> #include<cstring> #include<cmath> #include<algor ...

  8. 洛谷——P1546 最短网络 Agri-Net

    P1546 最短网络 Agri-Net 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助. 题目描述 约翰已经给他的农场安排了一 ...

  9. vue-alioss-组件封装

    <template> <div class="vui_alioss_upload"> <div @click="uloadImg()&quo ...

  10. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...