选课
2019-05-16 20:22 一只弱鸡丶 阅读(146) 评论(0) 编辑 收藏 举报#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <cstdlib> using namespace std; #define ll long long #define re register inline void read(int &a) { a=0; int d=1; char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch^48; while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48); a*=d; } struct note { int v,w,next; }edge[605]; int head[605],num,f[305][305],m; inline void add(int u,int v,int w) { edge[++num].next=head[u]; edge[num].v=v; edge[num].w=w; head[u]=num; } inline void dfs(int u,int fa) { for(re int i=head[u];i;i=edge[i].next) { if(edge[i].v!=fa) { dfs(edge[i].v,u); for(re int j=m;j>0;j--) for(re int k=0;k<j;k++) f[u][j]=max(f[u][j],f[u][j-k-1]+f[edge[i].v][k]+edge[i].w); } } } int main() { int n; read(n); read(m); for(re int i=1;i<=n;i++) { int u,w; read(u); read(w); add(u,i,w); add(i,u,w); } dfs(0,-1); printf("%d",f[0][m]); return 0; }