博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P3047 [USACO12FEB]附近的牛Nearby Cows
阅读量:5291 次
发布时间:2019-06-14

本文共 2969 字,大约阅读时间需要 9 分钟。

题目描述

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.

农民约翰已经注意到他的奶牛经常在附近的田野之间移动。考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛,而且是为了从附近的田地里去吃草的奶牛。

具体来说,FJ的农场由N块田野构成(1 <= n <= 100,000),每两块田野之间有一条无向边连接(总共n-1条边)。FJ设计了农场,任何两个田野i和j之间,有且只有一条路径连接i和j。第 i块田野是C(i)头牛的住所,尽管奶牛们有时会通过k条路到达其他不同的田野(1<=k<=20)。

FJ想在每块田野上种上够M(i)头奶牛吃的草。M(i)指能从其他点经过最多k步就能到达这个点的奶牛的个数。

现给出FJ的每一个田野的奶牛的数目,请帮助FJ计算每一块田野的M(i)。

输入格式

* 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)

第一行:n和k;

后面n-1行:i和j(两块田野);

之后n行:1..n每一块的C(i);

输出格式

* Lines 1..N: Line i should contain the value of M(i).

n行:每行M(i);//i:1..2

输入输出样例

输入 #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.

题目简述:给出一棵n个点的树,每个点上有C_i头牛,问每个点k步范围内各有多少头牛。

感谢@Slager_Z 提供翻译

 

#include
#include
#include
#include
#include
#include
using namespace std;struct B{ int t,ne,d;}a[200005];int n,e,fr[100005],c[100005],s[100005],maxs[100005],sum;void add(int f,int t,int d){ a[++e].t=t; a[e].ne=fr[f]; fr[f]=e; a[e].d=d;}void treedp(int fa,int u){ s[u]=c[u]; for (int i=fr[u];i;i=a[i].ne) if (a[i].t!=fa) { treedp(u,a[i].t); s[u]+=s[a[i].t]; maxs[u]=max(maxs[u],s[a[i].t]); } maxs[u]=max(maxs[u],sum-s[u]);}void dfs(int fa,int u)//求一个最短路而已,因为s到这里已经没用了我就节约一下{ //哦对了我没有初始化是因为只有唯一路径,只被搜到一次,不需要取min,直接覆盖原值就行 for (int i=fr[u];i;i=a[i].ne) if (fa!=a[i].t) { s[a[i].t]=s[u]+a[i].d; dfs(u,a[i].t); }}int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&c[i]),sum+=c[i]; for (int i=1,x,y,z;i

  

转载于:https://www.cnblogs.com/xiongchongwen/p/11243546.html

你可能感兴趣的文章
欲望都市游戏设计 背景图层和UI图层的设计
查看>>
2-2 groovy基础知识-理论介绍
查看>>
Null Object Design Pattern (Python recipe)
查看>>
bootstrap学习笔记(6)
查看>>
leetcode : Valid Sudoku
查看>>
浅谈-Lambda
查看>>
storm 批处理(窗口)
查看>>
洛谷 P1052 过河
查看>>
Python3 从零单排28_线程队列&进程池&线程池
查看>>
java resources 红叉 Cannot change version of project facet Dynamic Web Module to 2.5
查看>>
阿里云 CentOS7.2 配置FTP+Node.js环境
查看>>
HttpWebRequest 发送简单参数
查看>>
Eclipse启动JVM机制
查看>>
一年的第几天
查看>>
leetcode 223: Rectangle Area
查看>>
Blender插件编写指南
查看>>
二次重建基本完成辣!
查看>>
PHP与Linux进程间的通信
查看>>
【长期更新】坑点合集
查看>>
wnmp windows 2012 r2+php7.0+nginx1.14安装
查看>>