/ Vijos / 题库 / 选课 P1180

5 4
0 1
1 1
2 3
0 3
2 4

Copy

9

Copy

7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2
Copy

13

Copy

huyichen

``````#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define modd ***
using namespace std;
int n,m;
int f[350][350];
int fa[350];
int val[350];
int root[350];
vector<int> g[1005];
void dfs(int u,int num){
//从u开始选，选择了num个物品的最优值
for(int i=0;i<g[u].size();++i){
int son=g[u][i];
for(int j=0;j<num;++j) f[son][j]=f[u][j]+val[son];
dfs(son,num-1);
for(int j=1;j<=num;++j){
f[u][j]=max(f[u][j],f[son][j-1]);
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
fa[i]=x;
val[i]=y;
g[x].push_back(i);
}
for(int i=1;i<=n;i++)  f[0][i]=0;
dfs(0,m);
printf("%d\n",f[0][m]);
return 0;
}``````

2022-12-07 12:01

2022-12-03 19:43

2022-12-29 20:49

2022-12-30 21:00

2022-12-16 16:59

2022-12-26 14:32

2022-12-06 13:30

2022-12-28 10:01

2022-12-06 12:24

2022-12-26 18:01

2022-12-26 18:53