zoukankan      html  css  js  c++  java
  • P3047 [USACO12FEB]附近的牛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的农场由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 提供翻译


    题意:统计树上每个点与其距离小于k的点权值和。

    其实儿子的数量还是很好解决的,但是兄弟节点包括祖先的兄弟节点的处理会有困难。

    一遍dfs似乎无法处理,所以考虑up and down更新。

    第一遍f[i][j]处理i节点的子树中j深度的权值和,这时因为根节点没有兄弟和祖先所以与根节点相距k距离的节点已经处理完成。考虑用这一点处理其他节点。

    设节点x,其父亲节点ff,则距ff节点2距离的节点包括距x节点3距离的兄弟,父亲的兄弟还有x子树中深度为1的节点。

    用g[i][j]表示与i节点距离为j的全部节点(f[i][j]表示的是i的子树中与i距离为j的节点)想要求出与x距离为i的节点只要用g[ff][i-1]-f[x][i-2]+f[x][i]即可


    #include<iostream>
    #include<stdio.h>
    
    using namespace std;
    
    int i,m,n,j,k,g,h,a[100005],ver[200005],nex[200005],head[100005],cnt,f[100005][21],w[100005][21],o;
    
    void add(int x,int y)
    {
    	cnt+=1;
    	ver[cnt]=y;
    	nex[cnt]=head[x];
    	head[x]=cnt;
    }
    
    void dfs(int x,int y)
    {
    	for(int i=head[x];i;i=nex[i])
    	{
    		int t=ver[i];
    		if(t==y) continue;
    		dfs(t,x);
    		for(int j=1;j<=k;j++) f[x][j]+=f[t][j-1];
    	}
    	f[x][0]=a[x];
    }
    
    void dfs1(int x,int y)
    {
    	for(int i=head[x];i;i=nex[i])
    	{
    		int t=ver[i];
    		if(t==y) continue;
    		w[t][0]=a[t]; w[t][1]=a[x]+f[t][1];
    		for(int j=2;j<=k;j++) w[t][j]=w[x][j-1]-f[t][j-2]+f[t][j];
    		dfs1(t,x);
    	}
    }
    
    int main()
    {
    	scanf("%d%d",&n,&k);
    	for(i=1;i<n;i++)
    	{
    		scanf("%d%d",&g,&h);
    		add(g,h); add(h,g);
    	}
    	for(i=1;i<=n;i++) scanf("%d",&a[i]);
    	dfs(1,0);
    	for(i=0;i<=k;i++) w[1][i]=f[1][i];
    	dfs1(1,0);
    	for(i=1;i<=n;i++) 
    		for(j=1;j<=k;j++) w[i][j]+=w[i][j-1];
     	for(i=1;i<=n;i++) printf("%d
    ",w[i][k]); 
    }
    
  • 相关阅读:
    (并查集)小希的迷宫 --HDU -- 1272
    (并查集)Connections in Galaxy War -- zoj --3261 还没写
    (并查集)A Bug's Life -- POJ -- 2492
    LINQ 图解 LINQ学习第三篇 [转]
    C# 4.0 新特性-dynamic 【转】
    C#编程规范
    C#中关于DateTime的最大值和最小值
    .NET,你忘记了么?(八)—— 从dynamic到特性误用 [转]
    C#中dynamic的正确用法【转】
    ASP.NET MVC 中将FormCollection与实体间转换方法【转】
  • 原文地址:https://www.cnblogs.com/ZUTTER/p/9308430.html
Copyright © 2011-2022 走看看