题目描述

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发现他的牛经常跑到附近的草地去吃草,FJ准备给每个草地种足够的草供这个草地以及附近草地的奶牛来吃。FJ有N个草地(1<=N<=100000),有N-1条双向道路连接这些草地,FJ精心设计了这些道路使每两个草地有且仅有一条简单路径连接。第i个草场有Ci头牛,有时候奶牛会走过K条道路到其他草地吃草。FJ想知道每个草场最多可能有的奶牛数量Mi,即所有走过K条道路后可能到达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)

输出

* 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


题解

树形dp

由于直接推出答案比较困难,所以我们可以现在子树中寻找答案,然后再求父树中。

设f[x][k]表示x子树中到x的距离为k的的点的点权之和,

显然有f[x][k]=∑f[to[i]][k-1]。

再设g[x][k]表示整棵树中到x的距离为k的点的点权之和。

首先有g[1][k]=f[1][k],g[x][0]=v[x],剩下的就只能从上到下dp了。

我们已经知道子树中的答案,差的就是父树。

而如果父树中的点到to[i]的距离为k,那么这些点到x的距离必然为k-1.

于是我们就可以通过g[x][k-1]推出g[to[i]][k]。

然而这样当k≥2时会重复计算子树中的某些点,这些点到x的为k-1,那么到to[i]的距离必然为k-2。

这样就再减去f[to[i]][k-2]即可。

最终状态转移方程就为g[to[i]][k]=f[to[i]][k]+g[x][k-1]-f[to[i]][k-2]。

最后答案就是∑g[i][j] (0≤j≤k)

#include <cstdio>
#include <algorithm>
using namespace std;
int f[100010][25] , g[100010][25] , c[100010] , head[100010] , to[200010] , next[200010] , cnt , k;
void add(int x , int y)
{
to[++cnt] = y;
next[cnt] = head[x];
head[x] = cnt;
}
void dp1(int x , int fa)
{
int i , j;
f[x][0] = c[x];
for(i = head[x] ; i ; i = next[i])
{
if(to[i] != fa)
{
dp1(to[i] , x);
for(j = 1 ; j <= k ; j ++ )
f[x][j] += f[to[i]][j - 1];
}
}
}
void dp2(int x , int fa)
{
int i , j;
for(i = head[x] ; i ; i = next[i])
{
if(to[i] != fa)
{
g[to[i]][0] = c[to[i]];
for(j = 1 ; j <= k ; j ++ )
{
g[to[i]][j] = f[to[i]][j] + g[x][j - 1];
if(j >= 2) g[to[i]][j] -= f[to[i]][j - 2];
}
dp2(to[i] , x);
}
}
}
int main()
{
int n , i , j , x , y , ans;
scanf("%d%d" , &n , &k);
for(i = 1 ; i < n ; i ++ )
scanf("%d%d" , &x , &y) , add(x , y) , add(y , x);
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &c[i]);
dp1(1 , 0);
for(i = 0 ; i <= k ; i ++ )
g[1][i] = f[1][i];
dp2(1 , 0);
for(i = 1 ; i <= n ; i ++ )
{
ans = 0;
for(j = 0 ; j <= k ; j ++ )
ans += g[i][j];
printf("%d\n" , ans);
}
return 0;
}

【bzoj2591】[Usaco 2012 Feb]Nearby Cows 树形dp的更多相关文章

  1. [Usaco 2012 Feb]Nearby Cows

    题目描述 FJ发现他的牛经常跑到附近的草地去吃草,FJ准备给每个草地种足够的草供这个草地以及附近草地的奶牛来吃.FJ有N个草地(1<=N<=100000),有N-1条双向道路连接这些草地, ...

  2. 洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

  3. luogu 3047 [USACO12FEB]附近的牛Nearby Cows 树形dp

    $k$ 十分小,直接暴力维护 $1$~$k$ 的答案即可. 然后需要用父亲转移到儿子的方式转移一下. Code: #include <bits/stdc++.h> #define M 23 ...

  4. [USACO12FEB] 附近的牛 Nearby Cows - 树形dp,容斥

    给你一棵 \(n\) 个点的树,点带权,对于每个节点求出距离它不超过 \(k\) 的所有节点权值和 \(m_i\) 随便定一个根,设\(f[i][j]\)表示只考虑子树,距离为\(j\)的权值和,\( ...

  5. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  6. [P2996][USACO10NOV]拜访奶牛Visiting Cows (树形DP)

    之前写在洛谷,结果没保存,作废…… 听说考前写题解RP++哦 思路 很容易想到是 树形DP 如果树形DP不知道是什么的话推荐百度一下 我在这里用vector储存边 设状态f[i][0]为i点不访问,f ...

  7. USACO 2012 Feb Cow Coupons

    2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...

  8. [USACO 2012 Feb Gold] Cow Coupons【贪心 堆】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=118 传送门2:http://www.lydsy.com/JudgeOn ...

  9. [luoguP3047] [USACO12FEB]附近的牛Nearby Cows(DP)

    传送门 dp[i][j][0] 表示点 i 在以 i 为根的子树中范围为 j 的解 dp[i][j][1] 表示点 i 在除去 以 i 为根的子树中范围为 j 的解 状态转移就很好写了 ——代码 #i ...

随机推荐

  1. Spring Cloud 分布式事务管理

    Spring Cloud 分布式事务管理 在微服务如火如荼的情况下,越来越多的项目开始尝试改造成微服务架构,微服务即带来了项目开发的方便性,又提高了运维难度以及网络不可靠的概率. Spring Clo ...

  2. MapWinGIS使用

    .net语言中使用MapWinGIS.ocx http://www.cnblogs.com/kekec/archive/2011/03/30/1999709.html 基于MapWinGis的开发探索 ...

  3. P1488 肥猫的游戏

    题目描述 野猫与胖子,合起来简称肥猫,是一个班的同学,他们也都是数学高手,所以经常在一起讨论数学问题也就不足为奇了.一次,野猫遇到了一道有趣的几何游戏题目,便拿给胖子看.游戏要求在一个有n个顶点凸多边 ...

  4. 北京Uber优步司机奖励政策(4月4日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. 成都Uber优步司机奖励政策(3月2日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 一个体验好的Windows 任务栏缩略图开发心得

    本文来自网易云社区 作者:孙有军 前言: 对于一个追求极致体验的软件来说,利用好系统的每一点优秀的特性,将会大大提高软件的品质. Windows vista以来任务栏缩略图,及Win + TAB的程序 ...

  7. 「日常训练&知识学习」树的直径(POJ-1849,Two)

    题意 一个城市由节点和连接节点的街道组成,街道是双向的. 此刻大雪覆盖了这个城市,市长确定了一些街道要将它们清扫干净,这些街道保证所有的节点可以通过它们连通而且街道数目尽可能小. 现有两台相同的扫雪机 ...

  8. TPO-13 C2 How to use language lab

    TPO-13 C2 How to use language lab 第 1 段 1.Listen to a conversation between a student and the languag ...

  9. Android开发-API指南-<permission-tree>

    <permission-tree> 英文原文:http://developer.android.com/guide/topics/manifest/permission-tree-elem ...

  10. ajax的$.get()方法和tomcat服务器的交互

    AJAX AJAX = 异步 JavaScript 和 XML. AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. Ajax  get()方法 定义和用法 $.get() 方法 ...