hdu The more, The Better
来源:互联网 发布:java pathfinder下载 编辑:程序博客网 时间:2024/06/07 21:02
把0作为根,然后dp
注意一点 就是以前是选取m个点 现在相当于选取了m+1个点
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;int dp[222][222];vector<int>mp[222];int val[222];int vis[222];int n,m;void dfs(int u){ vis[u]=1; dp[u][1]=val[u]; for(int i=0;i<mp[u].size();i++){ int v = mp[u][i]; if(vis[v]) continue; dfs(v); for(int j=m+1;j>1;j--){ for(int k=1;k<j;k++){ dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]); } } }}int main(){ while(scanf("%d%d",&n,&m)==2){ if(n==0&&m==0) break; for(int i=0;i<=n;i++){ mp[i].clear(); } for(int i=1;i<=n;i++){ int a,b; scanf("%d%d",&a,&b); mp[a].push_back(i); val[i]=b; } memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); vis[0]=1; dfs(0); printf("%d\n",dp[0][m+1]); }}
0 0
- hdu-The more, The Better
- hdu The more, The Better
- hdu The more, The Better
- The more, The Better HDU
- The more, The Better HDU
- hdoj(hdu)-1561-the more the better
- hdu 1561 The more,The better
- HDU 1516 The more, The Better
- HDU 4379 - The More The Better
- [统计] hdu 4379 the more the better
- hdu 4379 The More The Better
- hdu 1561 the more,the better
- Hdu 1561 The more, The Better
- HDU 1561 The more, The Better
- HDU 1561 The more, The Better
- HDU-1561-The more, The Better
- HDU 1561 The more, The Better
- HDU 4379 The More The Better
- 第十二周 项目三 日期时间类
- webcrawler-----Jsoap爬虫美女图片多线程
- HDFS的Java API
- 算法导论 用两个队列实现一个栈 10.1-7
- 使用node-inspector 调试nodejs程序
- hdu The more, The Better
- GridView的一些特殊属性:
- PHP简单MVC架构
- 通过mongodump和mongorestore实现Mongodb备份和恢复
- 嵌入式 linux下利用backtrace追踪函数调用堆栈以及定位段错误
- Ext.data.TreeStore
- Ubuntu 14.04操作系统信任链(IMA)扩展分析实验
- 一个值得终生铭记的计算题
- DOS下常用命令