http://acm.hdu.edu.cn/showproblem.php?pid=1561
树形DP+01背包
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const double eps=1e-8;
const double INF=1e50;
//const double pi=acos(-1);
#define N 205
struct point
{
int v;
vector<int> a;
}p[N];
int f[N][N],n,m;
bool vis[N];
void ser(int x)
{
vis[x]=false;
int i,j,k,t;
for (i=0;i<p[x].a.size();i++)
{
t=p[x].a[i];
if (vis[t])
{
ser(t);
for (k=m+1;k>=2;k--)
for (j=1;j<k;j++)
if (f[x][k-j]+f[t][j]>f[x][k])
f[x][k]=f[t][j]+f[x][k-j];
}
}
}
int main()
{
freopen("a","r",stdin);
int i,j,k;
while (scanf("%d%d",&n,&m))
{
if (n==0 && m==0) break;
for (i=0;i<=n;i++) p[i].a.clear();
int q;
memset(f,0,sizeof(f));
memset(vis,true,sizeof(vis));
for (i=1;i<=n;i++)
{
scanf("%d%d",&q,&p[i].v);
p[q].a.push_back(i);
f[i][1]=p[i].v;
}
vis[0]=false;
ser(0);
printf("%d\n",f[0][m+1]);
}
return 0;
}