洛谷 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+容斥原理。
错因:数组开小了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 100010
using namespace std;
int n,y,tot;
int val[MAXN],into[MAXN],f[MAXN][];
int to[MAXN*],net[MAXN*],head[MAXN*];
void add(int u,int v){
to[++tot]=v;net[tot]=head[u];head[u]=tot;
to[++tot]=u;net[tot]=head[v];head[v]=tot;
}
int main(){
//freopen("young.in","r",stdin);
//freopen("young.out","w",stdout);
scanf("%d%d",&n,&y);
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
into[u]++;
into[v]++;
}
for(int i=;i<=n;i++) scanf("%d",&val[i]);
for(int i=;i<=n;i++) f[i][]=val[i];
for(int j=;j<=y;j++)
for(int i=;i<=n;i++){
for(int k=head[i];k;k=net[k])
f[i][j]+=f[to[k]][j-];
if(j>) f[i][j]-=(into[i]-)*f[i][j-];
else f[i][]+=f[i][];
}
for(int i=;i<=n;i++)
cout<<f[i][y]<<endl;
}
洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows的更多相关文章
- 【题解】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( ...
- 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...
- 洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)
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 ...
- [USACO12FEB]附近的牛Nearby Cows
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...
- 【[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 ...
随机推荐
- 设置utf8编码问题
注意:乱码和request的具体实现类有关,现在已经查到的是RequestDispatcher.forward调用前使用的是org.apache.catalina.connector.RequestF ...
- bash脚本编程知识储备
bash脚本编程: 脚本程序:解释器解释执行: 首先得理清一些琐碎的知识点,我尽量把我所学的帮朋友一起梳理一下 编程环境:(我会在接下来的篇章,图文例子三结合的方式带大家一起学习) ...
- Java多线程演示样例(模拟通话,sleep,join,yield,wait,notify,Semaphore)
主线程等待子线程的多种方法 synchronized浅析 sleep 是静态方法,Thread.sleep(xx)谁调用谁睡眠. join 是合并方法.当前线程调用其它线程xx.join()则等到xx ...
- SDUT 1225-编辑距离(串型dp)
编辑距离 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 如果字符串的基本操作仅为:删除一个字符.插入一个字符和将一个字符改动 ...
- 痛苦的人生——JRuby on Rails的开发与部署小记
最近单位领导部署了一项开发用户自助服务系统的任务,该任务有且仅有我一人独立完成——哈哈,十分美妙的工作呢. 恰巧楼主最近被Ruby的美妙特性所迷惑,于是义无反顾地投入到Ruby on Rails的怀抱 ...
- c语言循环案例
do while #include <stdio.h> #include <stdlib.h> int main() { int a = 1,b = 10; do { b -= ...
- ubuntu16.04下配置caffe无GPU
1. 安装依赖项 1 sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5- ...
- [SCOI 2005] 栅栏
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1082 [算法] 首先二分“最多得到的符合条件的木板数”,检验时可以使用DFS,但是, ...
- Vmware 安装samba之二
安装samba:sudo apt-get install samba 安装smbclient:sudo apt-get install 安装smbfs:sudo apt-get smbfs 2.修改配 ...
- 解析RecyclerView(2)——带顶部View和底部View的RecyclerView
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...