luogu

Sol

阶段和状态都是树形DP板子题,这里只讲一下背包的部分(转移)叭

它其实是一个分组背包模型,具体理解如下:

对于一个结点x,它由它的子结点y转移而来

在子结点y为根的树中可以选不同数量的课程,这些就可以看成一个组内的物品

具体来说,f[y][1],f[y][2],f[y][3]...这些都是一个组里的,只能选一样

注意每组内的物品只能选一样,所以枚举体积要倒序

over!

Code

 #include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
int r()
{
int x=,y=;;char ch;
ch=getchar();
while(ch<''||ch>'') {if(ch=='-') y=-;ch=getchar();}
while(ch>=''&&ch<='') {x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*y;
}
int n,m;
vector<int> son[];
int s[];
int f[][];
void dp(int x)
{
//f[x][0]=0;
for(int i=;i<son[x].size();i++)
{
int y=son[x][i];
dp(y);
for(int a=m;a>=;a--)//倒序!
for(int b=;b<=a;b++)
f[x][a]=max(f[x][a],f[x][a-b]+f[y][b]);
}
if(x!=)
{
for(int i=m;i>;i--)
f[x][i]=f[x][i-]+s[x];
}
}
int main()
{
n=r();m=r();
for(int i=;i<=n;i++)
{
int x=r();s[i]=r();
son[x].push_back(i);
}
//memset(f,0xcf,sizeof(f));
dp();
cout<<f[][m];
return ;
}

最新文章

  1. sql特殊语句
  2. 《OOC》笔记(2)——C语言实现trycatchfinally
  3. wind.onload和$(document).ready()的区别例示
  4. linux redis迁移
  5. 虚拟化之vmware-vsphere (web) client
  6. VR就是下一个浪潮_2016 (GMGC) 全球移动游戏大会观后感
  7. Youth(青春)
  8. 软件工程——PairProject
  9. 为oracle中的表格增加列和删除列
  10. Linux软连接和硬链接(摘录)
  11. java编程思想,对象导论
  12. BZOJ1057 [ZJOI2007]棋盘制作(极大化思想)
  13. scala学习笔记——操作符
  14. CJSON parse.c
  15. 自定义类使用泛型and方法使用泛型
  16. luogu1979 华容道 (dijkstra+bfs)
  17. NLTK 知识整理
  18. hasura graphql-engine &amp;&amp;patroni docker-compose 环境运行
  19. is not writable or has an invalid setter method错误的解决
  20. Hadoop学习笔记(1):WordCount程序的实现与总结

热门文章

  1. 使用Laravel5做权限管理
  2. LeetCode69 Sqrt(x)
  3. laravel进阶系列--通过事件和事件监听实现服务解耦
  4. NoSQL之简介
  5. 云原生生态周报 Vol. 7 | Docker 再爆 CVE
  6. @bzoj - 4709@ 柠檬
  7. SVN的使用与教程
  8. Shell 基本运算符 1
  9. OracleSpatial函数
  10. 微信公众号无法使用css3的多行省略