洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)
P3047 [USACO12FEB]附近的牛Nearby Cows
题目描述
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.
给出一棵n个点的树,每个点上有C_i头牛,问每个点k步范围内各有多少头牛。
输入输出格式
输入格式:
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)
输出格式:
- Lines 1..N: Line i should contain the value of M(i).
输入输出样例
6 2
5 1
3 6
2 4
2 1
3 2
1
2
3
4
5
6
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.
/*
树形dp:dp[i][j]:编号为i的节点向子节点走0~j步总和
预处理dp[i][j]数组,从当前点向父亲节点转移
容斥原理:ans+=dp[now][k],ans+=dp[father][k-1],ans-=dp[now][k-2] 不懂可手动模拟
*/
#include<iostream>
#include<cstdio>
#include<cstring> #define N 100007
#define M 27 using namespace std;
int w[N],f[N],head[N],dp[N][M];
int n,m,k,cnt;
struct edge
{
int u,to,pre;
}e[N<<]; inline int init()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} inline void add(int u,int to)
{
e[++cnt].to=to;e[cnt].pre=head[u];head[u]=cnt;
} void dfs(int from,int now)
{
f[now]=from;dp[now][]=w[now];
for(int i=head[now];i;i=e[i].pre)
{
if(e[i].to!=from)
{
dfs(now,e[i].to);
for(int j=;j<=k;j++)
dp[now][j]+=dp[e[i].to][j-];
}
}
} void DP(int now)
{
int K=k,ans=;ans=dp[now][k];
while(now!= && K)
{
K--;ans+=dp[f[now]][K];
if(K) ans-=dp[now][K-];
now=f[now];
}
printf("%d\n",ans);
} int main()
{
int x,y;
n=init();k=init();
for(int i=;i<n;i++)
{
x=init();y=init();
add(x,y);add(y,x);
}
for(int i=;i<=n;i++) w[i]=init();
dfs(-,);
for(int i=;i<=n;i++) for(int j=;j<=k;j++)//dfs时dp[i][j]是第i点刚好走j步,现在求前缀和
dp[i][j]+=dp[i][j-];
for(int i=;i<=n;i++) DP(i);
return ;
}
洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)的更多相关文章
- 洛谷 P3177 [HAOI2015]树上染色 树形DP
洛谷 P3177 [HAOI2015]树上染色 树形DP 题目描述 有一棵点数为 \(n\) 的树,树边有边权.给你一个在 \(0 \sim n\)之内的正整数 \(k\) ,你要在这棵树中选择 \( ...
- 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
- 【bzoj2591】[Usaco 2012 Feb]Nearby Cows 树形dp
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...
- 洛谷P1040 加分二叉树(树形dp)
加分二叉树 时间限制: 1 Sec 内存限制: 125 MB提交: 11 解决: 7 题目描述 设一个n个节点的二叉树tree的中序遍历为(l,2,3,...,n),其中数字1,2,3,...,n ...
- 洛谷P4438 道路 [HNOI/AHOI2018] 树形dp
正解:树形dp 解题报告: 传送门! 昂首先看懂题目趴QwQ大概就是说有棵满二叉树,有n个叶子节点(乡村)和n-1个非叶子节点,然后这棵树的每个节点有三个属性abc,对每个非叶子节点可以从与子节点的两 ...
- 洛谷 P4201 设计路线 [NOI2008] 树形dp
正解:树形dp 解题报告: 大概是第一道NOI的题目?有点激动嘻嘻 然后先放个传送门 先大概港下这题的题意是啥qwq 大概就是给一棵树,然后可以选若干条链把链上的所有边的边权变成0,但是这些链不能有交 ...
- 洛谷 P3267 [JLOI2016/SHOI2016]侦察守卫(树形dp)
题面 luogu 题解 树形\(dp\) \(f[x][y]表示x的y层以下的所有点都已经覆盖完,还需要覆盖上面的y层的最小代价.\) \(g[x][y]表示x子树中所有点都已经覆盖完,并且x还能向上 ...
- 洛谷P1351 联合权值(树形dp)
题意 题目链接 Sol 一道很简单的树形dp,然而被我写的这么长 分别记录下距离为\(1/2\)的点数,权值和,最大值.以及相邻儿子之间的贡献. 树形dp一波.. #include<bits/s ...
- 洛谷P4099 [HEOI2013]SAO(树形dp)
传送门 HEOI的题好珂怕啊(各种意义上) 然后考虑树形dp,以大于为例 设$f[i][j]$表示$i$这个节点在子树中排名第$j$位时的总方案数(因为实际只与相对大小有关,与实际数值无关) 我们考虑 ...
随机推荐
- 3.用Redis Desktop Manager连接Redis(CentOS)
Redis Desktop Manager是Redis图形化管理工具,方便管理人员更方便直观地管理Redis数据. 然而在使用Redis Desktop Manager之前,有几个要素需要注意: 一. ...
- 微信小程序音频长度获取的问题
小程序推荐使用wx.createInnerAudioContext()创建的innerAudioContext,我们也通过这个接口创建音频.音频的长度可以通过属性获取: 但是,给innerAudioC ...
- @viewChild
https://www.cnblogs.com/mttcug/p/8004359.html
- todey
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> 框架集fromset ...
- EF-Lamdba
一丶基本语法 var userList=db.set<table>().where(c=>c.id=="001"&&c.userName.Cont ...
- Linux之加密(基于key认证、建立私有云CA)
对称加密: 一般的加密是用一个密码加密文件,解密用同样的密码,加密解密用一把密钥 非对称加密: 一个密码加密文件,解密却用另外一组密码,意思就是加密解密的密码不一样,其结果就是用这一组密钥中的一个来加 ...
- isset在php5.6-和php7.0+的一些差异
今天在公司实现一个模块功能时写了如下代码: class ProductCategory { const TYPES = [ 1 => 'type1', 2 => 'type2', ]; p ...
- Python之类方法,lambda,闭包简谈
类方法,lambda,闭包 类方法 lambda 闭包 类方法 classmethod staticmethod instancemethod 类方法 类方法,通过装饰器@classmethod来标明 ...
- ubuntu16.04 安装 eclipse
从官网下载 eclipse 的 linux 版本 eclipse-cpp-neon-1a-linux-gtk-x86_64.tar.gz 直接解压就能得到一个可运行的IDE,但是直接点击 eclips ...
- hdu2003 求绝对值【C++】
求绝对值 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...