[BZOJ2591][Usaco 2012 Feb]Nearby Cows
Posted Elder_Giang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[BZOJ2591][Usaco 2012 Feb]Nearby Cows相关的知识,希望对你有一定的参考价值。
2591: [Usaco 2012 Feb]Nearby Cows
Time Limit: 4 Sec Memory Limit: 128 MB Submit: 149 Solved: 89 [Submit][Status][Discuss]Description
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发现他的牛经常跑到附近的草地去吃草,FJ准备给每个草地种足够的草供这个草地以及附近草地的奶牛来吃。FJ有N个草地(1<=N<=100000),有N-1条双向道路连接这些草地,FJ精心设计了这些道路使每两个草地有且仅有一条简单路径连接。第i个草场有Ci头牛,有时候奶牛会走过K条道路到其他草地吃草。FJ想知道每个草场最多可能有的奶牛数量Mi,即所有走过K条道路后可能到达i的奶牛总数。
Input
* 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)
Output
* Lines 1..N: Line i should contain the value of M(i).
Sample Input
5 1
3 6
2 4
2 1
3 2
1
2
3
4
5
6
Sample Output
21
16
10
8
11
#include <cstdio> #include <cstring> char buf[10000000], *ptr = buf - 1; inline int readint(){ int n = 0; while(*++ptr < ‘0‘ || *ptr > ‘9‘); while(*ptr <= ‘9‘ && *ptr >= ‘0‘) n = (n << 1) + (n << 3) + (*ptr++ & 15); return n; } const int maxn = 100000 + 10, maxk = 20 + 5; struct Edge{ int to, next; Edge(){} Edge(int _t, int _n): to(_t), next(_n){} }e[maxn * 2]; int fir[maxn] = {0}, cnt = 0; inline int ins(int u, int v){ e[++cnt] = Edge(v, fir[u]); fir[u] = cnt; e[++cnt] = Edge(u, fir[v]); fir[v] = cnt; } int n, k; int w[maxn]; int f[maxn][maxk] = {0}, g[maxn][maxk]; void dfs1(int u, int fa){ // for(int i = 1; i <= k; i++) f[u][i] = 0; f[u][0] = w[u]; for(int v, i = fir[u]; i; i = e[i].next){ v = e[i].to; if(v == fa) continue; dfs1(v, u); for(int j = 1; j <= k; j++) f[u][j] += f[v][j - 1]; } for(int i = 0; i <= k; i++) g[u][i] = f[u][i]; } void dfs2(int u, int fa){ for(int v, i = fir[u]; i; i = e[i].next){ v = e[i].to; if(v == fa) continue; g[v][1] += w[u]; for(int j = 2; j <= k; j++) g[v][j] += g[u][j - 1] - f[v][j - 2]; dfs2(v, u); } } int main(){ buf[fread(buf, sizeof(char), sizeof(buf), stdin)] = 0; n = readint(); k = readint(); for(int i = 1; i < n; i++) ins(readint(), readint()); for(int i = 1; i <= n; i++) w[i] = readint(); dfs1(1, 0); dfs2(1, 0); for(int sum, i = 1; i <= n; i++){ sum = 0; for(int j = 0; j <= k; j++) sum += g[i][j]; printf("%d\n", sum); } return 0; }
以上是关于[BZOJ2591][Usaco 2012 Feb]Nearby Cows的主要内容,如果未能解决你的问题,请参考以下文章
BZOJ2590: [Usaco2012 Feb]Cow Coupons
BZOJ 3942: [Usaco2015 Feb]Censoring
bzoj3940 [Usaco2015 Feb]Censoring
BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚