[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+容斥原理;
代码实现
- #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的更多相关文章
- 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...
- 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
- 【洛谷3047】[USACO12FEB]附近的牛Nearby Cows
题面 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into acc ...
- 【题解】Luogu p3047 [USACO12FEB]附近的牛Nearby Cows 树型dp
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...
- LUOGU P3047 [USACO12FEB]附近的牛Nearby Cows
传送门 解题思路 树形dp,看到数据范围应该能想到是O(nk)级别的算法,进而就可以设出dp状态,dp[x][j]表示以x为根的子树,距离它为i的点的总和,第一遍dp首先自底向上,dp出每个节点的子树 ...
- P3047 [USACO12FEB]附近的牛Nearby Cows
https://www.luogu.org/problemnew/show/P304 1 #include <bits/stdc++.h> 2 #define up(i,l,r) for( ...
- 【[USACO12FEB]附近的牛Nearby Cows】
我记得我调这道题时中耳炎,发烧,于是在学长的指导下过了也没有发题解 发现我自己的思路蛮鬼畜的 常规操作:\(f[i][j]\) 表示到\(i\)的距离为\(j\)的奶牛有多少只,但注意这只是在第二遍d ...
- [luoguP3047] [USACO12FEB]附近的牛Nearby Cows(DP)
传送门 dp[i][j][0] 表示点 i 在以 i 为根的子树中范围为 j 的解 dp[i][j][1] 表示点 i 在除去 以 i 为根的子树中范围为 j 的解 状态转移就很好写了 ——代码 #i ...
- luogu 3047 [USACO12FEB]附近的牛Nearby Cows 树形dp
$k$ 十分小,直接暴力维护 $1$~$k$ 的答案即可. 然后需要用父亲转移到儿子的方式转移一下. Code: #include <bits/stdc++.h> #define M 23 ...
随机推荐
- OAuth2.0认证流程是如何实现的?
导读 大家也许都有过这样的体验,我们登录一些不是特别常用的软件或网站的时候可以使用QQ.微信或者微博等账号进行授权登陆.例如我们登陆豆瓣网的时候,如果不想单独注册豆瓣网账号的话,就可以选择用微博或者微 ...
- Effective Java读书笔记完结啦
Effective Java是一本经典的书, 很实用的Java进阶读物, 提供了各个方面的best practices. 最近终于做完了Effective Java的读书笔记, 发布出来与大家共享. ...
- java自动包装与解包
关于java的自动包装机制想必大家都用过吧,一般这些机制都用于在往容器中存储基本类型数据的时候,因为容器中不允许存在基本数据类型,所以就会调用自动包装机制,将基本数据类型转换为对象,将基本数据保存在对 ...
- java之java.lang.UnsupportedClassVersionError:com/mysql/jdbc/Driver : Unsupported major.minor version 52.0
问题解释:jdk版本和mysql驱动版本不兼容,比如:jdk1.7与mysql-connector-java-5.xxx兼容,但与mysql-connector-java-6.xxx及以上不兼容
- Map接口框架图
Java集合大致可分为Set.List和Map三种体系,其中Set代表无序.不可重复的集合:List代表有序.重复的集合:而Map则代表具有映射关系的集合.Java 5之后,增加了Queue体系集合, ...
- 内存管理总结-autoreleasePool
转自其他 序言 无论是在MRC时期还是ARC时期,做过开发的程序员都接触过autoreleasepool.尽管接触过但本人对它还不是很了解.本文只是将自己的理解说出来.在内存管理的文章中提到了OC的内 ...
- ajax 请求spring之post
# 背景 现在使用spring boot开发一个web应用是非常普遍的了,ajax请求更是标配:那么你在ajax请求时,是否遇到过在controller中获取不到参数的情况呢?特别是post请求: # ...
- js 零散知识
# 同一种类型的事件注册多个事件句柄,后面的不会覆盖前面的事件 # event.which == 13,13代表回车 # parsley.js验证框架 # JSON.stringify, avoid ...
- IDEA常见问题
IDEA常见问提解决 一:拉取git代码认证失败(无法重新输入账户和密码) git config --system --unset credential.helper 二:取消新建文件自动添加到S ...
- html页面比较长,如何用js实现网页一打开显示在网页的中部?
加入js代码 <style type="text/css"> body { height: 2000px; } </style> <script ty ...