hdu 1561 The more, The Better(树形dp)
来源:互联网 发布:ssd优化 编辑:程序博客网 时间:2024/05/29 19:34
dp[root][i]root代表这是第几个节点,i代表在root这个节点攻陷的城市数量
#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>#define maxn 1000010using namespace std;int dp[210][210],fa[210],bro[210],son[210],c[210];int n,p;void dfs(int root){ int k=son[root]; dp[root][1]=c[root]; while(k) { dfs(k); for(int i=p;i>=1;i--) { int q=i; if(root==0)q=i+1;//如果节点为0,特殊处理,0和其他节点的边不能计算进去 for(int j=0;j<q;j++) { dp[root][i]=max(dp[root][i-j]+dp[k][j],dp[root][i]); } } k=bro[k]; }}int main(){ int a;while(~scanf("%d%d",&n,&p)&&n){memset(fa,0,sizeof(fa));memset(bro,0,sizeof(bro));memset(son,0,sizeof(son));memset(dp,0,sizeof(dp));c[0]=0;for(int i=1;i<=n;i++){ scanf("%d%d",&a,&c[i]); bro[i]=son[a]; fa[i]=a; son[a]=i;}dfs(0);int ans=dp[0][p];printf("%d\n",ans);} return 0;}
0 0
- HDOJ (HDU) 1561 The more, The Better (树形DP)
- hdu 1561 The more, The Better(树形DP)
- hdu 1561 The more, The Better(树形DP入门)
- hdu 1561 the more the better (树形DP)
- HDU 1561 The more, The Better(树形dp+背包)
- HDU 1561 The more, The Better(树形DP)
- hdu 1561 The more, The Better(树形DP)
- HDU 1561 The more, The Better(树形DP)
- hdu 1561 The more, The Better(树形DP)
- hdu 1561 The more, The Better(树形dp)
- HDU 1561 The more, The Better (树形DP)
- hdu 1561 The more, The Better (树形DP)
- hdu 1561 The more, The Better 树形DP+背包
- hdu 1561 The more, The Better(树形dp)
- hdu 1561 (树形dp)The more, The Better
- HDU 1561 The more, The Better(树形DP)
- hdu 1561 The more, The Better(树形DP入门)
- hdu 1561 The more, The Better 树形DP入门题
- 2017河南工业大学校赛 D rqy的键盘
- NCS8801S规格书RGB/LVDS转EDP
- 查找表【严蔚敏】
- uva 441 Lotto
- linux中自建证书搭建https
- hdu 1561 The more, The Better(树形dp)
- LinkedHashMap和HashMap的比较使用
- AngularJS入门 、AngularJS控制器 、AngularJS常见的内置指令
- angular--$watch监视用法
- org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: u near line 1, column 55 [fro
- angular入门
- Redis系列(三)——主从介绍和配置
- 有关虚函数的注意事项浅谈
- Quartz定时调度