The more, The Better
依赖背包+树形dp
f[x][j+1]=max(f[x][j+1],f[x][j+1-k]+f[i->n][k]);
我的一个疑问就是这k个节点会不会选重复,答案是不会的,因为从一开始就不会重复,根据dp原理回溯的时候过程是一样的,也是不会重复的

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 100010
#define For(i,a,b) for(int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar() using namespace std;
int f[][];
int a[];
int n,num;
int x,y,m; struct node{
int n;
node *next;
}*e[]; void in(int &x){
int y=;
char c=g();x=;
while(c<''||c>''){
if(c=='-')y=-;
c=g();
}
while(c<=''&&c>=''){
x=(x<<)+(x<<)+c-'';c=g();
}
x*=y;
}
void o(int x){
if(x<){
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void dfs(int x,int m){ f[x][]=a[x];
for(node *i=e[x];i;i=i->next){
if(m>)
dfs(i->n,m-);
for(int j=m-;j;j--)
For(k,,j)
f[x][j+]=max(f[x][j+],f[x][j+-k]+f[i->n][k]);
}
} void push(int x,int y){
node *p;
p=new node();
p->n=y;
if(e[x]==)
e[x]=p;
else{
p->next=e[x]->next;
e[x]->next=p;
}
} int main(){
while(cin>>n>>m&&n+m){
memset(f,,sizeof(f));
For(i,,n)
e[i]=; For(i,,n){
in(x);in(a[i]);
push(x,i);
}
dfs(,m+);
o(f[][m+]);p('\n');
}
return ;
}

随机推荐

  1. 使用Node.js+Hexo+Github搭建个人博客(续)

    一.写在前面 在我的上一篇博客<使用Nodejs+Hexo+Github搭建个人博客>中,已经介绍了如何使用 Hexo 在 Github Pages 上搭建一个简单的个人博客.该篇博文将在 ...

  2. Confluence 6 当前使用的数据库状态

    进入  > 基本配置(General Configuration) > 问题检查和支持工具(Troubleshooting and support tools) 你就可以看到当前使用的数据 ...

  3. Confluence 6 降级你的许可证

    如果你决定降级你 Confluence 的许可证而削减你的许可证开支,你需要确定当前已经直排的用户许可证数量(在用户许可证页面中)要少于你希望应用的新的许可证的允许用户数量,在你应用新许可证的时候. ...

  4. 第四周学习总结-HTML

    2018年8月5日 这是暑假第四周,这一周我在菜鸟教程网学到了许多HTML的知识.HTML编写网页不像C语言.Java语言那必须有主方法.主函数什么的,它基本上都是标签(元素),但是它可以与CSS(层 ...

  5. SSM 三大框架---事务处理

    SSM 三大框架---事务处理 原创 2016年05月12日 20:57:03 标签: spring / J2EE / java / 框架 / 事务 7010 在学习三大框架的时候,老师说事务处理是最 ...

  6. hive sql常用整理-hive引擎设置

    遇到个情况,跑hive级联insert数据报错,可以尝试换个hive计算引擎 hive遇到FAILED: Execution Error, return code 2 from org.apache. ...

  7. Python Day-1 练习

    作业1 要求:1.输入用户密码 2.认真成功后显示欢迎信息 3.输入三次锁定 代码如下: __author__ = 'zhang.ning' username = "zhangning&qu ...

  8. error: each element of 'ext_modules' option must be an Extension instance or 2-tuple

    在编译cython扩展时出现. 解决办法: 必须先import setup再import extension,否则报错 from setuptools import setup from distut ...

  9. Senparc.Weixin微信开发(3) 自定义菜单与获取用户组

    自定义菜单 代码参考:http://www.cnblogs.com/szw/p/3750517.html 还可以使用他们官网的自定义:https://neuchar.senparc.com/User/ ...

  10. Centos6安装SaltStack

    rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm yum install ...