[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

6 2
5 1
3 6
2 4
2 1
3 2
1
2
3
4
5
6

Sample Output

15
21
16
10
8
11
 
一开始以为$k\le n$吓尿了。。。
仔细看$k\le 20$,所以这题就很水了
设$f[i][j]$为$i$为根的子树中走$j$步刚好能走到$i$的牛有多少
然后显然$f[u][j]=\sum f[son][j-1]$,dfs即可
但是这样会漏掉从父亲节点走过来的牛
那么我们再设$g[i][j]$表示整棵树中走$j$步刚好能走到$i$的牛有多少
那么再dfs一遍
对于每个节点$u$,$g[u][j]=f[u][j]+g[fa][j-1]-g[u][j-2]$
最后点$i$答案为$\sum_{0\le j\le k}g[i][j]$
#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;
}

 

 
posted @ 2017-10-07 19:53  jzyy  阅读(374)  评论(0编辑  收藏  举报