zoukankan      html  css  js  c++  java
  • [Usaco 2012 Feb]Nearby Cows

    [Usaco 2012 Feb]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. 
    FJ发现他的牛经常跑到附近的草地去吃草,FJ准备给每个草地种足够的草供这个草地以及附近草地的奶牛来吃。FJ有N个草地(1<=N<=100000),有N-1条双向道路连接这些草地,FJ精心设计了这些道路使每两个草地有且仅有一条简单路径连接。第i个草场有Ci头牛,有时候奶牛会走过K条道路到其他草地吃草。FJ想知道每个草场最多可能有的奶牛数量Mi,即所有走过K条道路后可能到达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) 

    输出

    * 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
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,k,tot,head[400005],f[400001][100];
     4 struct data {
     5     int to,nxt;
     6 } e[400005];
     7 void build(int x,int y) {
     8     e[++tot].to=y;
     9     e[tot].nxt=head[x];
    10     head[x]=tot;
    11 }
    12 void dfs(int x,int fa) {
    13     for(int i=head[x]; i; i=e[i].nxt) {
    14         int v=e[i].to;
    15         if(v==fa)
    16             continue;
    17         dfs(v,x);
    18         for(int j=1; j<=k; j++)
    19             f[x][j]+=f[v][j-1];
    20     }
    21 }
    22 void dp(int x,int fa) {
    23     for(int i=head[x]; i; i=e[i].nxt) {
    24         int v=e[i].to;
    25         if(v==fa)
    26             continue;
    27         for(int j=k; j>=2; j--)
    28             f[v][j]-=f[v][j-2];
    29         for(int j=1; j<=k; j++)
    30             f[v][j]+=f[x][j-1];
    31         dp(v,x);
    32     }
    33 }
    34 int main() {
    35     scanf("%d%d",&n,&k);
    36     for(int i=1; i<n; i++) {
    37         int x,y;
    38         scanf("%d%d",&x,&y);
    39         build(x,y);
    40         build(y,x);
    41     }
    42     for(int i=1,x; i<=n; i++)
    43         scanf("%d",&x),f[i][0]=x;
    44     dfs(1,0);
    45     dp(1,0);
    46     for(int i=1; i<=n; i++) {
    47         int ans=0;
    48         for(int j=0; j<=k; j++)
    49             ans+=f[i][j];
    50         printf("%d
    ",ans);
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    unity调用Android的两种方式:其二,调用aar包
    Unity调用Android的两种方式:其一、调用jar包
    使用 Gradle 编译 Java 项目时报错: Could not find Tools.jar
    Unity CommandInvokationFailure: Failed to re-package resources. 解决方案
    Unity Android路径及注意事项
    Unity Debug类
    Unity C#集合
    Unity C# const与static readonly的区别与联系
    Unity 脚本中各种[XXX]的用法
    unity 看到Sphere内部,通过Sphere播放全景视频时候遇到的问题
  • 原文地址:https://www.cnblogs.com/sbwll/p/13395172.html
Copyright © 2011-2022 走看看