题目

分析

又是一个显然的树形依赖背包

然而可以 \(O(nm)\) 依靠 \(dfs\) 序来 \(dp\)

\(Code\)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int N = 2005;
int v[N] , p[N] , f[N][10005] , n , m , tot , h[N] , dfn[N] , dfc , siz[N];
struct edge{
int to , nxt;
}e[N * 2 + 5]; void add(int x , int y){e[++tot] = edge{y , h[x]} , h[x] = tot;}
void dfs(int x , int fa)
{
dfn[++dfc] = x , siz[x] = 1;
for(register int i = h[x]; i; i = e[i].nxt)
{
int y = e[i].to;
if (y == fa) continue;
dfs(y , x);
siz[x] += siz[y];
}
} int main()
{
scanf("%d%d" , &n , &m);
int x , y;
for(register int i = 1; i <= n; i++) scanf("%d%d" , &v[i] , &p[i]);
for(register int i = 1; i < n; i++) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x);
dfs(1 , 0);
for(register int i = n; i >= 1; i--)
for(register int j = m; j >= 0; j--)
{
if (j >= p[dfn[i]]) f[i][j] = max(f[i][j] , f[i + 1][j - p[dfn[i]]] + v[dfn[i]]);
f[i][j] = max(f[i][j] , f[i + siz[dfn[i]]][j]);
}
printf("%d" , f[1][m]);
}

JZOJ 5344. 【NOIP2017模拟9.3A组】摘果子的更多相关文章

  1. [jzoj 5343] [NOIP2017模拟9.3A组] 健美猫 解题报告 (差分)

    题目链接: http://172.16.0.132/senior/#main/show/5343 题目: 题解: 记旋转i次之后的答案为$ans_i$,分别考虑每个元素对ans数组的贡献 若$s_i& ...

  2. JZOJ 5246. 【NOIP2017模拟8.8A组】Trip(trip)

    5246. [NOIP2017模拟8.8A组]Trip(trip) (File IO): input:trip.in output:trip.out Time Limits: 1500 ms Memo ...

  3. JZOJ 5235. 【NOIP2017模拟8.7A组】好的排列

    5235. [NOIP2017模拟8.7A组]好的排列 (File IO): input:permutation.in output:permutation.out Time Limits: 1000 ...

  4. JZOJ 5236. 【NOIP2017模拟8.7A组】利普希茨

    5236. [NOIP2017模拟8.7A组]利普希茨 (File IO): input:lipschitz.in output:lipschitz.out Time Limits: 1000 ms ...

  5. JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠

    JZOJ [NOIP2017提高A组模拟9.14]捕老鼠 题目 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕 ...

  6. JZOJ 5230. 【NOIP2017模拟A组模拟8.5】队伍统计

    5230. [NOIP2017模拟A组模拟8.5]队伍统计 (File IO): input:count.in output:count.out Time Limits: 1500 ms Memory ...

  7. JZOJ 4273. 【NOIP2015模拟10.28B组】圣章-精灵使的魔法语

    4273. [NOIP2015模拟10.28B组]圣章-精灵使的魔法语 (File IO): input:elf.in output:elf.out Time Limits: 1000 ms  Mem ...

  8. JZOJ 3509. 【NOIP2013模拟11.5B组】倒霉的小C

    3509. [NOIP2013模拟11.5B组]倒霉的小C(beats) (File IO): input:beats.in output:beats.out Time Limits: 1000 ms ...

  9. JZOJ 3508. 【NOIP2013模拟11.5B组】好元素

    3508. [NOIP2013模拟11.5B组]好元素(good) (File IO): input:good.in output:good.out Time Limits: 2000 ms  Mem ...

  10. JZOJ 4272. 【NOIP2015模拟10.28B组】序章-弗兰德的秘密

    272. [NOIP2015模拟10.28B组]序章-弗兰德的秘密 (File IO): input:frand.in output:frand.out Time Limits: 1000 ms  M ...

随机推荐

  1. redis集群之分片集群的原理和常用代理环境部署

    上篇文章刚刚介绍完redis的主从复制集群,但主从复制集群主要是为了解决redis集群的单点故障问题,通过整合哨兵能实现集群的高可用:但是却无法解决数据容量以及单节点的压力问题,所以本文继续介绍red ...

  2. 【Java SE】Day02 数据类型转换、运算符、方法入门

    一.数据类型转换 1.自动转换 取值范围小在运算时会提升为取值范围大的类型 byte+int=int int+double=double 转换规则:byte.short.char-->int-- ...

  3. 【Java EE】Day01 基础加强、Junit单元测试、反射、注解

    〇.总结 1.测试:三个注解.断言判断 2.反射:三个阶段获取字节码对象的三种方式.忽略成员变量权限方法setAccessible(true) 3.注解:内置注解SupressWarning& ...

  4. Curve 块存储应用实践 -- iSCSI

    Curve 是云原生计算基金会 (CNCF) Sandbox 项目,是网易数帆发起开源的高性能.易运维.云原生的分布式存储系统. 为了让大家更容易使用以及了解 Curve,我们期望接下来通过系列应用实 ...

  5. Python matplotlib 学习——建立画布和坐标系

    #导入包import matplotlib.pyplot as plt #让图表在jupyter展示出来%matplotlib inline#解决中文乱码问题plt.rcParams["fo ...

  6. .NET周报【12月第1期 2022-12-08】

    国内文章 CAP 7.0 版本发布通告 - 支持延迟消息,性能炸了? https://www.cnblogs.com/savorboard/p/cap-7-0.html) 今天,我们很高兴宣布 CAP ...

  7. 04.Javascript学习笔记3

    1.箭头函数 箭头函数是一种更短的函数表达式. const age = birthyear => 2022 - birthyear; console.log(age(2000)) 箭头左边的bi ...

  8. Docker容器入门到精通

    Docker 容器 快速入门 第一章:Docker容器 第二章:Dockerfile指令与Docker-compose容器编排-搭建docker私有仓库 h1 { color: rgba(0, 60, ...

  9. MySQL事务(四大特征)-存储过程

    目录 一:事务 1.四大特性(ACID) 2.事物存在的必要性(真实比喻) 3.如何使用事务 4.开启事务-回滚-确认 二:事务案例实战 1.模拟消费 2.创建 3.插入数据 4.开启事务 5.修改操 ...

  10. 知识分享-消息中间件详解+rabbitMQ

    知识分享-消息中间件详解+rabbitMQ 消息中间件 概述 消息中间件是基于队列与消息传递技术,在网络环境中为应用系统提供同步或异步.可靠的消息传输的支撑性软件系统. 应用场景 异步处理 对于电商a ...