洛谷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$位时的总方案数(因为实际只与相对大小有关,与实际数值无关) 我们考虑 ...
随机推荐
- HDU_1542_线段树【扫描线】
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- block要用copy修饰,还是用strong
栈区与堆区 block本身是像对象一样可以retain,和release.但是,block在创建的时候,它的内存是分配在栈(stack)上,而不是在堆(heap)上.他本身的作于域是属于创建时候的作用 ...
- 关于在win7旗舰版32位上 安装 net4.0 的闪退问题研究 和安装sqlserver2008问题
1.配置文件客户端[目标x86x64]的 可以安装 2.配置文件完全的目标x86x64的 出现闪退. 3.服务器核心的出现无法安装 安装 sqlserver 2008R2数据库 报错 \最后留下了它, ...
- R包
查看默认安装包的位置 .libPaths() 移除包 remove.packages("package_name") 查看所有安装的包 library() 按 q 退出包列表 ...
- 关于Staltstack
saltstate服务搭建: cat /etc/hosts(master和minion都添加) 127.0.0.1 localhost localhost.localdomain localhos ...
- 想学Python?这里有一个最全面的职位分析
Python从2015年开始,一直处于火爆的趋势,目前Python工程师超越Java.Web前端等岗位,起薪在15K左右,目前不管是小公司还是知名大公司都在热招中. 当然,每个城市对岗位的需求也不尽相 ...
- nginx+tomcat实现负载均衡集群
一台tomcat站点由于可能出现单点故障及无法应付过多客户复杂多样的请求等问题,不能单独应用于生产环境下 所以需要一套可靠的解决方案来完善web站点架构 而Nginx只能访问静态页面,如果需要动态需要 ...
- join 和 left join 和 right join的区别?
join等价于 inner join 是内连接 ,返回两个表都有的符合条件的行. left join 是左连接,返回坐表中所有的行以及右表中符合条件的行. right join右连接,是返回右表中所有 ...
- ACM蓝桥杯之换硬币问题
题目描述: 想兑换100元零钱,有1元.2元.5元.10元四种面值,总有多少种兑换方法? 解题思路: 本题可以采用多种方法求解.最容易理解的应该就是暴力穷举和递归求解.那么本文主要介绍这两种解法. 暴 ...
- Dinic当前弧优化 模板及教程
在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...