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. Confluence 6 PostgreSQL 创建数据库和数据库用户

    一旦你成功的安装了 PostgreSQL 数据库: 创建一个数据库用户,例如 confluenceuser. 你的新用户必须能够  创建数据库对象(create database objects) 和 ...

  2. vue.js的计算机属性学习

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 【python】升级pip后报错解决pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application

    原本使用pip版本为7.1.0,后升级至9.0.1 之后使用pip list提示pkg_resources.DistributionNotFound: The 'pip==7.1.0' distrib ...

  4. NMT 机器翻译

    本文近期学习NMT相关知识,学习大佬资料,汇总便于后期复习用,有问题,欢迎斧正. 目录 RNN Seq2Seq Attention Seq2Seq + Attention Transformer Tr ...

  5. java常见错误总结

    1. 现象:将数组转为List后进行removeAll()操作,报java.lang.UnsupportedOperationException错误. 代码: /** * 获取标记ID * @retu ...

  6. bitset用法详解

    参见此博客: https://www.cnblogs.com/magisk/p/8809922.html

  7. Spring声明式事务@Transactional 详解,事务隔离级别和传播行为

    @Transactional注解支持9个属性的设置,这里只讲解其中使用较多的三个属性:readOnly.propagation.isolation.其中propagation属性用来枚举事务的传播行为 ...

  8. Mysql 查看连接数,状态 最大并发数

    show status like '%max_connections%'; ##mysql最大连接数set global max_connections=1000 ##重新设置show variabl ...

  9. Python-数据类型之数字

    一:数字类型概述 数字提供了标量存储和直接访问,属于不可变数据类型,所谓不可变,我们可以认为,更改数字的值会生成一个新的对象 # id可以唯一表示一个对象 age =18 print(id(age)) ...

  10. Sqoop使用,mysql,hbase,hive等相互转换

    Sqoop 是一款用来在不同数据存储软件之间进行数据传输的开源软件,它支持多种类型的数据储存软件. 安装 Sqoop 1.下载sqoop并加mysql驱动包 http://mirror.bit.edu ...