题目:
思路分析:
dp[i][j] 代表以i为根的树上选j个课程的最大得分
代码实现:
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define re register //局部
#define PI atan(1.0)*4
#define ull unsigned long long
#define scf(n) scanf("%d",&n)
#define scfl(n) scanf("%lld",&n)
#define prf(n) printf("%d",n)
#define prfl(n) printf("%lld",n)
#define scfd(n) scanf("%lf",&n)
#define prfd(n) printf("%.lf",n)
#define prf10(n) printf("%.10f",n)
#define ls (rt<<1)
#define rs (rt<<1|1)
//#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int INF = 0x3f3f3f3f;
const double EPS=1e-10;
const double Pi=3.1415926535897;
using namespace std;
const int MAX=310;
int n,m;
int w[MAX];
vector<int>e[MAX];
int vis[MAX];
int f[MAX][MAX];
void dfs(int u){
vis[u]=1;
f[u][1]=w[u];
for(int i=0;i<e[u].size();i++){
int v=e[u][i];
if(vis[v]) continue;
dfs(v);
for(int j=m+1;j>0;j--){
for(int k=0;k<j;k++){
f[u][j]=max(f[u][j],f[u][j-k]+f[v][k]);
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
w[i]=y;
e[x].push_back(i);
}
dfs(0);
cout<<f[0][m+1];
}