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

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. python里transmap_c++学习笔记-map的使用实例(单词转换)
  2. 如何创造char二叉树C语言,递归创建二叉树c语言实现+详细解释
  3. C#实现动态分配IP和释放IP
  4. 【Java网络编程(四)】手写TCP聊天室——控制台版
  5. Elasticsearch7.X ILM索引生命周期管理(冷热分离)
  6. java list 字段去重_如何实现java8 list按照元素的某个字段去重
  7. ERP项目实施记录01
  8. 数值的八进制、十六进制表示,及以二进制进行显示
  9. java里程碑之泛型--使用泛型
  10. php steam 第三方登录,Steam第三方登录
  11. python矩阵计算器心得_矩阵类计算器Python
  12. Python3入门视频教程百度网盘
  13. 自定义控件 TextView 歌词 Lrc
  14. 封装的PKPM BimView的方法
  15. Zynq-PS-SDK(4) 之 GIC 配置
  16. EasySwoole 基础入门
  17. 程序猿来做一下这套试卷,看看你的实力如何!
  18. 2023计算机CCF A类会议期刊截稿日期
  19. 千寻定位服务接入指南
  20. taglib指令标签的详细使用

热门文章

  1. dhtmlxTree 10分钟做一个树
  2. 深度学习《Photo Editing》
  3. VC++ 6.0(英文正版)安装及其编译注意事项
  4. 使你的C/C++代码支持Unicode
  5. Mosquitto 0 15 开源MQTT v3 1 Broker
  6. Spring AOP技术(基于AspectJ)的XML开发
  7. Namomo Spring Camp Div2 Week1 - 第一次打卡
  8. I—爆炸的符卡洋洋洒洒(不模不行的DP)
  9. linux 内核模型,The Linux Kernel Device Model - Overview -- Linux 内核设备模型概述
  10. 2021年河南高考成绩排名查询一分一段表,2021年河南高考文科一分一段表,河南一分一段表文科位次排名查询...