题目

分析

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

然而可以 \(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. 学习ASP.NET Core Blazor编程系列十五——查询

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  2. 【Shell案例】【小数点scale&bc】14、求平均值

    描述写一个bash脚本以实现一个需求,求输入的一个的数组的平均值 第1行为输入的数组长度N第2~N行为数组的元素,如以下为:数组长度为4,数组元素为1 2 9 8示例:41298 那么平均值为:5.0 ...

  3. C++日期和时间编程总结

    一,概述 二,C-style 日期和时间库 2.1,数据类型 2.2,函数 2.3,数据类型与函数关系梳理 2.4,时间类型 2.4.1,UTC 时间 2.4.2,本地时间 2.4.3,纪元时间 2. ...

  4. GP之gpbackup备份

    从GP6.0后,使用gpbackup命令来实现备份.但GP里是不自带的,需要自己重新下载并编译和安装. 一.安装 (1)master上go下载并配置profile环境变量 go下载地址 :https: ...

  5. python 之选择结构(if --elif --else)

    python中有三种结构:顺序结构.选择结构.循环结构,此处介绍选择结构. if -- else 结构: if 判断条件: 执行语句 else: 执行语句 当if后面的判断条件为真(True)时,执行 ...

  6. python 水仙花数、菱形、99乘法表、直角三角形

    空心菱形 i = 1 while i <= 3: # 控制行数 j = 1 k = 1 while j <= 3-i: # 控制空格数量 print(" ", end= ...

  7. 用Echarts实现SpreadJS引用从属关系可视化

    在金融行业,我们经常会有审计审查的需求,对某个计算结果进行审查,但是这个计算结果可能依赖多个单元格,而且会有会有多级依赖的情况,如果让我们的从业人员靠眼睛找,工作量巨大,而且准确性存疑,基本上死路一条 ...

  8. csp-j 游记

    ### 初赛 day -7 ~ day -1 赛前集训,都很简单,什么二叉树,图论呀,轻松搞定.做了 $2008$ 至 $2015$ 年的普及组真题,都在 $50$ 分以上,感觉初赛稳了(坐标 $HN ...

  9. MongoDB从入门到实战之MongoDB工作常用操作命令

    前言: 上一章节我们快速的在Docker容器中安装了MongoDB,并且通过Navicat MongoDB可视化管理工具快速的连接.创建数据库.集合以及添加了文档数据源.这一章节我们主要是了解一下在日 ...

  10. 安装pytorch-gpu的经验与教训

    首先说明 本文并不是安装教程,网上有很多,这里只是自己遇到的一些问题 我是以前安装的tensorflow-gpu的,但是发现现在的学术论文大部分都是用pytorch复现的,因此才去安装的pytorch ...