题目描述

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).

输入输出样例

输入样例#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.

思路

树形DP+容斥原理;

代码实现

 #include<cstdio>
const int maxn=1e5+;
const int maxm=2e5+;
int n,m;
int f[maxn][],ft[maxn];
int s[maxn],ans[maxn];
int h[maxn],hs;
int et[maxm],en[maxm];
void add(){
int a,b;
scanf("%d%d",&a,&b);
et[++hs]=b,en[hs]=h[a],h[a]=hs;
et[++hs]=a,en[hs]=h[b],h[b]=hs;
}
void dfs(int k,int fa){
ft[k]=fa;
for(int i=;i<=m;i++) f[k][i]+=s[k];
for(int i=h[k];i;i=en[i])
if(et[i]!=fa){
dfs(et[i],k);
for(int j=;j<=m;j++){
f[k][j]+=f[et[i]][j-];
}
}
}
int lca(int k,int son,int now){
int ret=;
while(k&&now>=){
ret-=f[son][now-],ret+=f[k][now];
son=k,k=ft[son],now--;
}
return ret;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) add();
for(int i=;i<=n;i++) scanf("%d",&s[i]);
dfs(,);
for(int i=;i<=n;i++){
printf("%d\n",lca(ft[i],i,m-)+f[i][m]);
}
return ;
}

[USACO12FEB]附近的牛Nearby Cows的更多相关文章

  1. 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...

  2. 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows

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

  3. 【洛谷3047】[USACO12FEB]附近的牛Nearby Cows

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

  4. 【题解】Luogu p3047 [USACO12FEB]附近的牛Nearby Cows 树型dp

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

  5. LUOGU P3047 [USACO12FEB]附近的牛Nearby Cows

    传送门 解题思路 树形dp,看到数据范围应该能想到是O(nk)级别的算法,进而就可以设出dp状态,dp[x][j]表示以x为根的子树,距离它为i的点的总和,第一遍dp首先自底向上,dp出每个节点的子树 ...

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

    https://www.luogu.org/problemnew/show/P304 1 #include <bits/stdc++.h> 2 #define up(i,l,r) for( ...

  7. 【[USACO12FEB]附近的牛Nearby Cows】

    我记得我调这道题时中耳炎,发烧,于是在学长的指导下过了也没有发题解 发现我自己的思路蛮鬼畜的 常规操作:\(f[i][j]\) 表示到\(i\)的距离为\(j\)的奶牛有多少只,但注意这只是在第二遍d ...

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

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

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

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

随机推荐

  1. 189 Rotate Array 旋转数组

    将包含 n 个元素的数组向右旋转 k 步.例如,如果  n = 7 ,  k = 3,给定数组  [1,2,3,4,5,6,7]  ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...

  2. Netflix正式开源其API网关Zuul 2--转

    微信公众号:聊聊架构 5 月 21 日,Netflix 在其官方博客上宣布正式开源微服务网关组件 Zuul 2.Netflix 公司是微服务界的楷模,他们有大规模生产级微服务的成功应用案例,也开源了相 ...

  3. linux下常用网络操作汇总 专题

    centos 更改主机名,需要更改的几个地方: (1) /etc/sysconfig/network  //更改主机名(2)/etc/hostname  //更改主机名(3) /etc/hosts   ...

  4. 浅析套接字中SO_REUSEPORT和SO_REUSEADDR的区别

    Socket的基本背景 在讨论这两个选项的区别时,我们需要知道的是BSD实现是所有socket实现的起源.基本上其他所有的系统某种程度上都参考了BSD socket实现(或者至少是其接口),然后开始了 ...

  5. 《基于Node.js实现简易聊天室系列之总结》

    前前后后完成这个聊天室的Demo花了大概一个星期,当然一个星期是仅仅指编码的工作.前期的知识储备是从0到1从无到有,花费了一定的时间熟悉Node.js的基本语法以及Node.js和mongoDB之间的 ...

  6. 微信小程序组件解读和分析:十一、label标签

    label标签组件说明: label标签,与html的label标签基本一样.label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label 元素内点击文本,就会触发 ...

  7. Ubuntu 几个常用的更新命令

    apt-cache search package 搜索包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt-get install package ...

  8. iTOP-4412开发板-LinuxC-继电器模块的测试例程

    平台:iTOP-4412开发板 实现:继电器模块测试例程 继电器的 C 的测试程序,C 测试程序可以在 Android系统,Qt 系统以及最小 linux 系统上运行,文档以 Android 系统上测 ...

  9. jQuery 首页搜索区域模块随页面滑动而变化

    /*搜索区块的颜色变化*/ function search(){ var searchBox = document.querySelector('.m_head'); var bannerBox = ...

  10. 阿里云服务器基本搭建_错误1_Permission denied (publickey)

    首先 修改这两个密码 然后重启服务器就可以了