洛谷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$位时的总方案数(因为实际只与相对大小有关,与实际数值无关) 我们考虑 ...
随机推荐
- Codeforces_733D
D. Kostya the Sculptor time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- HDU_Reward_拓扑排序
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- day13-迭代器、三元表达式、列表推导式、字典生成式、生成器与递归
目录 迭代器 可迭代对象 迭代器对象 for循环原理 三元表达式(三目表达式) 列表推导式 字典生成式 zip()方法 生成器 生成器表达式 递归 递归的两个阶段 迭代器 迭代器即迭代的工具,迭代是一 ...
- PHP 之微信JSSDK类封装
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appS ...
- ionic错误
1. 问题:Error: read ECONNRESET 启动使用ionic serve启动服务器之后只要一刷新界面就会导致服务器关闭,报的错误如下: events.js:136 throw er; ...
- 第七节:numpy之矩阵及特殊矩阵的创建
- Marshal.ReleaseComObject() vs. Marshal.FinalReleaseComObject()
很简单,不翻译了. If you are using COM components on your .NET code, you might be already aware of the Marsh ...
- vue开发规范
一.简介 团队合作中规范文档是必须的,在多人合作的项目只有定义好一定的编码规范才会使得开发井井有序,代码一目了然,下边将谈一下个人对vue使用规范的一些看法. 二.规范案例 1.组件命名 组件文件名应 ...
- noip模拟赛 括号序列
题目描述LYK有一个括号序列,但这个序列不一定合法.一个合法的括号序列如下:()是合法的括号序列.若A是合法的括号序列,则(A)是合法的括号序列.若A和B分别是合法的括号序列,则AB是合法的括号序列. ...
- hdu_1049_Climbing Worm_201311061331
Climbing Worm Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...