完全没有思路,看了题解还有些迷

f[i][j]表示和节点\(i\)距离为\(j\)的节点的总数,\(son\)为\(i\)的儿子,\(num\)为儿子的数量
则转移方程为f[i][j] = f[s][j-1] - f[i][j-2] * (num - 1)
这样可以保证不重不漏

\(DP\)顺序也要注意一下,难得没用深搜来转移

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define LL long long
LL read() {LL k = 0, f = 1; char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9')k = k * 10 + c - 48, c = getchar();return k * f;
}
int head[100010], tot;
struct zzz {int f, t, nex;
}e[100010 << 1];
void add(int x, int y) {e[++tot].t = y;e[tot].f = x;e[tot].nex = head[x];head[x] = tot;
}
int f[100010][21], n, k;
int main() {n = read(), k = read();for(int i = 1; i <= n - 1; ++i) {int x = read(), y = read();add(x, y); add(y, x);}for(int i = 1; i <= n; ++i)f[i][0] = read();for(int j = 1; j <= k; ++j)for(int i = 1; i <= n; ++i) {int num = 0;for(int s = head[i]; s; s = e[s].nex)f[i][j] += f[e[s].t][j-1], ++num;if(j > 1) f[i][j] -= f[i][j-2] * (num-1);else f[i][j] += f[i][0];}for(int i = 1; i <= n; ++i)printf("%d\n", f[i][k]);return 0;
}

转载于:https://www.cnblogs.com/wxl-Ezio/p/11052381.html

USACO12FEB Nearby Cows相关推荐

  1. P3047 [USACO12FEB]附近的牛Nearby Cows

    题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...

  2. NOIP复健计划——动态规划

    树形DP [POI2011] DYN-Dynamite 二分 K K K check(mid): 能否选出 m m m个点,使得 ∀ i 为关键点, M i n j i s s e l e c t e ...

  3. linux删除多余日志,linux 删除日志

    https://jingyan.baidu.com/album/c1a3101e73129ade656deb9d.html?picindex=2 里面的 ls -s 可以看到目录 https://zh ...

  4. 动态规划--from zpz

    版权声明:copy from zpz,我可能要修改 https://blog.csdn.net/qq_40828060/article/details/83064425 文章目录 前言 记忆化搜索 动 ...

  5. 动态规划,且学且放弃

    文章目录 前言 记忆化搜索 动态规划的基本解题思路 背包问题 01背包 (待处理)P1489 猫狗大战 01背包的空间优化问题 P1048 采药 P1510 精卫填海 P1566 加等式 P1504 ...

  6. 洛谷P3048 [USACO12FEB]牛的IDCow IDs

    P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 ...

  7. Milking Cows 挤牛奶

    1.2.1 Milking Cows 挤牛奶 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 554  Solved: 108 [Submit][Stat ...

  8. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 1 /* 2 二分搜索:搜索安排最近牛的距离不小于d 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #incl ...

  9. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

最新文章

  1. 函数参数传递常用的三种方式
  2. 干货丨从线性回归到无监督学习,数据科学家需要掌握的十大统计技术
  3. title: bat批处理简介:Windows自动化之道
  4. stripfxg php,代码审计| 适合练手的ZZCMS8.2漏洞
  5. linux18.04循环登陆,ubuntu18.04 循环登陆
  6. Azure 5月新发布:CDN图片处理功能, CDN Restful API, 新版CDN管理门户, 计量名称变更延期
  7. 法学界四大主流“数据权利与权属”观点
  8. 关于sql 拼接字符串的问题
  9. Java 打印某年某月的月日历
  10. sql语句之多表查询
  11. 短视频剪辑如何入门?给初学者的三个小建议,助你快速入门
  12. Math.h 正态分布 C语言,C++与正态分布(示例代码)
  13. 多边形的单边裁剪算法-JS
  14. 区块链溯源技术是什么
  15. 前端代码规范——CSS代码规范
  16. 计算机网络背诵笔记,震惊!网络搜索、数字笔记和云盘,让你变笨!!!
  17. 1032 挖掘机技术哪家强 Python实现
  18. 自制贴纸图案大全图片_贴纸的制作方法
  19. 计算机二级access分数分布_计算机二级access评分标准
  20. PC端Windows安装adb工具和使用

热门文章

  1. Python中的顺序表介绍
  2. reportviewer动态数据源
  3. ReportViewer教程(4)-为报表打造数据源
  4. python最小公倍数 菜鸟_Python实现的求解最小公倍数算法示例
  5. Origin2017绘制3D饼状图 去掉指定图例
  6. libsvm在matlab中使用的常见错误及libsvm的使用
  7. Excel2010--在指定的范围内添加随机数
  8. 百练 06 股票买卖
  9. D1. All are Same
  10. 坯子库安装不上_柜式七氟丙烷的安装调试方法