题目描述

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>
#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的更多相关文章

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

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

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

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

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

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

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

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

  5. 洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)

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

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

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

  7. [USACO12FEB]附近的牛Nearby Cows

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

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

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

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

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

随机推荐

  1. H3C SecPath U200-S 如何在内网使用外网IP地址访问内网服务器

    H3C SecPath U200-S 如何在内网使用外网IP地址访问内网服务器 ------------------------------------------------------------ ...

  2. NetOps Defined

    https://www.logzilla.net/2017/06/20/the-network-operations-top-5.html

  3. windows系统中软件开发常用的软件

    1.windwos快速打开控制面板:热键+r打开运行框,输入control就打开windows的控制面板了 2.windows自带的远程桌面控制系统:mstsc -Microsoft terminal ...

  4. [React Router] Prevent Navigation with the React Router Prompt Component

    In this lesson we'll show how to setup the Prompt component from React Router. We'll prompt with a s ...

  5. 路由器wiff设置

    1.将一根网线连接至路由wankou 2.将另外一根网页连接1.2.3.4口中一个,另外一个连接至电脑 3.登录192.168.1.1,进行设置向导选择ppoe,然后登录网络设置无线名称+密码 4.保 ...

  6. Windows 7 x64环境下JDK8安装过程

    Windows 7 x64环境下JDK8安装过程 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads ...

  7. Unity3D摄像机尾随人物

    这里的镜头主要是从人物的背后尾随的. 首先新建一个C#脚本,命名为MyFollow,然后把下面代码粘贴进去.保存: using UnityEngine; using System.Collection ...

  8. 深刻理解Java中的String、StringBuffer和StringBuilder的差别

    声明:本博客为原创博客,未经同意.不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(链接为http://blog.csdn.net/bettarwang/article/detai ...

  9. 0x25 广度优先搜索

    今天莫名不想说话. 结果发现效率挺高? poj3322 本来可以1a的..发现我宽搜写的是head<=tail而且初始是head=1,tail=2如果是多组数据简直就gg了.基础不牢固 这题虽然 ...

  10. iris中间件

    最近使用golang写的时候涉及到权限校验,用中间件(使用iris框架内的东西) 自己摸索出一种自己的方式 iris.UseFunc(MiddlewareFunc)使用这个方法,会在所有的请求之前执行 ...