选课

树上背包问题 每一个子树就是一组背包 由于最后选根节点 因此根节点可以看做是必须选的一组 才有了后面的递推

void dfs(int x,int fa)
{
	f[x][0]=0;
	for(int i=head[x];i;i=e[i].next)
	{
		int y=e[i].to;
		if(y==fa) continue;
		dfs(y,x);
		for(int t=m;t>=1;t--)	
			for(int j=0;j<=t;j++)
				f[x][t]=max(f[x][t],f[x][t-j]+f[y][j]);
	}
	if(x!=0)
		for(int t=m;t>=1;t--)
			f[x][t]=f[x][t-1]+val[x]; 
}
posted @ 2022-02-09 11:21  __iostream  阅读(33)  评论(0)    收藏  举报