【题解】Luogu p3047 [USACO12FEB]附近的牛Nearby Cows 树型dp
题目描述
Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but also for cows visiting from nearby fields.
Specifically, FJ’s farm consists of N fields (1 <= N <= 100,000), where some pairs of fields are connected with bi-directional trails (N-1 of them in total). FJ has designed the farm so that between any two fields i and j, there is a unique path made up of trails connecting between i and j. Field i is home to C(i) cows, although cows sometimes move to a different field by crossing up to K trails (1 <= K <= 20).
FJ wants to plant enough grass in each field i to feed the maximum number of cows, M(i), that could possibly end up in that field — that is, the number of cows that can potentially reach field i by following at most K trails. Given the structure of FJ’s farm and the value of C(i) for each field i, please help FJ compute M(i) for every field i.
农民约翰已经注意到他的奶牛经常在附近的田野之间移动。考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛,而且是为了从附近的田地里去吃草的奶牛。
具体来说,FJ的农场由N块田野构成(1 <= n <= 100,000),每两块田野之间有一条无向边连接(总共n-1条边)。FJ设计了农场,任何两个田野i和j之间,有且只有一条路径连接i和j。第 i块田野是C(i)头牛的住所,尽管奶牛们有时会通过k条路到达其他不同的田野(1<=k<=20)。
FJ想在每块田野上种上够M(i)头奶牛吃的草。M(i)指能从其他点经过最多k步就能到达这个点的奶牛的个数。
现给出FJ的每一个田野的奶牛的数目,请帮助FJ计算每一块田野的M(i)。
输入输出格式
输入格式:
Line 1: Two space-separated integers, N and K.
Lines 2..N: Each line contains two space-separated integers, i and j (1 <= i,j <= N) indicating that fields i and j are directly connected by a trail.
Lines N+1..2N: Line N+i contains the integer C(i). (0 <= C(i) <= 1000)
第一行:n和k;
后面n-1行:i和j(两块田野);
之后n行:1..n每一块的C(i);
输出格式:
- Lines 1..N: Line i should contain the value of M(i).
n行:每行M(i);//i:1..2
输入输出样例
输入样例#1: 复制
6 2
5 1
3 6
2 4
2 1
3 2
1
2
3
4
5
6
输出样例#1: 复制
15
21
16
10
8
11
说明
There are 6 fields, with trails connecting (5,1), (3,6), (2,4), (2,1), and (3,2). Field i has C(i) = i cows.
Field 1 has M(1) = 15 cows within a distance of 2 trails, etc.
题目简述:给出一棵n个点的树,每个点上有C_i头牛,问每个点k步范围内各有多少头牛。
思路
树型dp + 简单容斥原理
- 用$f[i][j]$表示
距节点i的距离小于等于j的所有节点的权值和,即牛的数量 - $son[i]$表示
i的子节点数 - 可以想到,将此状态转移至与自己连通的v节点的$f[v][j-1]$中,即 $f[i][j]=f[v][j-1]+c[i]$
但是,这样会有一部分重复计算。因为$f[v][j-1]$中还包括了
点i和与点i距离小于等于j-2的点
- 只要再减掉重复位置,即$f[i][j-2]*(son[i]-1)$
$$f[i][j]=f[s][j-1]-f[i][j-2]*(son[i]-1)$$
代码
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
const int maxn=1e5+50;
inline int read(){
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x*w;
}
struct data {
int to,nxt;
}edge[210001]; int head[maxn],tot;
int f[maxn][21],sons[maxn];
int n,k;
inline void add(int a,int b) {
edge[++tot].to=b;
edge[tot].nxt=head[a];
head[a]=tot;
edge[++tot].to=a;
edge[tot].nxt=head[b];
head[b]=tot;
sons[a]++; sons[b]++;
}
int main() {
re i,j,s;
n=read(),k=read();
int a,b;
for(i=1;i<n;++i) {
a=read(),b=read();
add(a,b);
}
for(i=1;i<=n;++i) f[i][0]=read();
for(j=1;j<=k;++j) for(i=1;i<=n;++i){
for(s=head[i];s;s=edge[s].nxt) f[i][j]+=f[edge[s].to][j-1];
if(j>1) f[i][j]-=(sons[i]-1)*f[i][j-2];
else f[i][1]+=f[i][0];
}
for(i=1;i<=n;++i) printf("%d\n",f[i][k]);
return 0;
}
【题解】Luogu p3047 [USACO12FEB]附近的牛Nearby Cows 树型dp的更多相关文章
- LUOGU P3047 [USACO12FEB]附近的牛Nearby Cows
传送门 解题思路 树形dp,看到数据范围应该能想到是O(nk)级别的算法,进而就可以设出dp状态,dp[x][j]表示以x为根的子树,距离它为i的点的总和,第一遍dp首先自底向上,dp出每个节点的子树 ...
- 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
- P3047 [USACO12FEB]附近的牛Nearby Cows
https://www.luogu.org/problemnew/show/P304 1 #include <bits/stdc++.h> 2 #define up(i,l,r) for( ...
- luogu 3047 [USACO12FEB]附近的牛Nearby Cows 树形dp
$k$ 十分小,直接暴力维护 $1$~$k$ 的答案即可. 然后需要用父亲转移到儿子的方式转移一下. Code: #include <bits/stdc++.h> #define M 23 ...
- LuoguP3047 [USACO12FEB]附近的牛Nearby Cows(树形DP,容斥)
\[f[u][step] = \begin{cases} C[u] & step = 0 \\ (\sum{f[v][step - 1]}) - f[u][step - 2] \cdot (d ...
- 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...
- 【洛谷3047】[USACO12FEB]附近的牛Nearby Cows
题面 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into acc ...
- [USACO12FEB]附近的牛Nearby Cows
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...
- 【[USACO12FEB]附近的牛Nearby Cows】
我记得我调这道题时中耳炎,发烧,于是在学长的指导下过了也没有发题解 发现我自己的思路蛮鬼畜的 常规操作:\(f[i][j]\) 表示到\(i\)的距离为\(j\)的奶牛有多少只,但注意这只是在第二遍d ...
随机推荐
- 从刚毕业的5K测试到20K测试大佬,与薪资相匹配的永远是实力!
有个话题"软件测试的工资高还是开发者的工资高?"软件测试和软件开发门槛有差异,在职业起步阶段收入也会有一定的差异,这算是行业内公开的秘密.但随着工作年限的增加,经验的逐步积累,软件 ...
- 那些好用的 VS Code 插件,究竟是如何提高编码效率的?
在上一篇文章中我们已经对 vscode 插件有了一个初步的认识与了解了,接下去我们就要"揭秘"一下市面上那些好用的 vscode 插件究竟是如何帮我们提高工作效率的. 本文首发于「 ...
- Pytorch_Part5_迭代训练
VisualPytorch beta发布了! 功能概述:通过可视化拖拽网络层方式搭建模型,可选择不同数据集.损失函数.优化器生成可运行pytorch代码 扩展功能:1. 模型搭建支持模块的嵌套:2. ...
- 干货!可以使用低代码平台代替Excel吗?
低代码开发平台可以代替Excel?不用惊讶,答案是肯定的,而且,低代码开发平台可以完全代替Excel.例如Zoho Creator低代码平台,可以围绕数据存储.管理和创建工作流程.期间不需要IT人员介 ...
- 论文笔记:RankIQA
0.Abstract 本文提出了一种从排名中学习的无参考图像质量评估方法(RankIQA).为了解决IQA数据集大小有限的问题,本文训练了一个孪生网络,通过使用合成的已知相对图像质量排名的数据集来训练 ...
- Linux USB ECM Gadget 驱动介绍
1 USB ECM介绍 USB ECM,属于USB-IF定义的CDC(Communication Device Class)下的一个子类:Ethernet Networking Control Mo ...
- [算法] 单链表插入排序(java)
实现 首先保证插入前的链表是个完整的,最后一个节点要断开 然后在插入前链表中找到比待插入节点大的最小元素,插到前面即可 Link.java class Link { private class Nod ...
- HTML的表格元素
一.HTML的表格元素 1.table元素 <table> 标签定义 HTML 表格.简单的 HTML 表格由 table 元素以及一个或多个 tr.th 或 td 元素组成.tr 元素定 ...
- ]# dmesg | grep ATAcentos下查看网卡,主板,CPU,显卡,硬盘型号等硬件信息
centos下查看网卡,主板,CPU,显卡,硬盘型号等硬件信息 osc_4o5tc4xq 2019/10/11 15:03 阅读数 253 centos下查看网卡,主板,CPU,显卡,硬盘型号等硬件信 ...
- 基于LNMP架构搭建wordpress个人博客
搭建过程 注意防火墙和selinux的影响可以先关闭. 一.安装nginx # 1.更改nginx源安装nginx [root@web01 ~]# vi /etc/yum.repos.d/nginx. ...