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