*点击

[Usaco 2012 Feb]Nearby Cows

题目

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 条边距离,所有点的权值和;

那么很明显是一道树形dp的题;

我们可以不断地用子节点更新父节点;

很显然父节点延伸的距离 += 子节点延伸的距离 -1 ;

那么   子节点延伸的距离 += 父节点延伸的距离 -1 ;

也就可以遍历两遍;

dp[x][j]+=dp[xx][j-1];     dp[xx][j]+=dp[x][j-1];

但是我们发现

第一次遍历时,到 x 距离为2的点,加上了了到 xx 距离为1 的点;

第二次遍历时,dp[xx][j] 加上 dp[x][j-1] 时  

举个例子,第一次遍历 dp[1][2]+ =dp[2][1];

                第二次遍历 dp[2][3]+ =dp[1][2];

在第一次遍历时 dp[1][2] 包括了 dp[2][1] 2的子树权值;

然鹅  ans在统计的时候加上了 dp[2][1]  2的子树权值;

第二次遍历 dp[2][3] 又加上了 dp[2][1];

ans 会统计dp[2][3],此时dp[2][3]包涵dp[2][1],而之前统计过一遍,所以要减去;

所以需要简单容斥一下;

 

for(re ll j=q;j>=2;j--)
    dp[xx][j]-=dp[xx][j-2];

 问题来了,为什么要逆循环呢;

因为如果正循环;

dp[xx][j]-=dp[xx][j-2];时

dp[xx][j-2] 就是一个已更新的值;

 

假如 dp[2][5]; 去求一个去重更新的值;

那么dp[2][5]-=dp[2][3];
如果此时的dp[2][3] 更新了,dp[2][3]就是一个减去dp[2][1] 的值;

 

dp[2][5] 再加的时候统计了dp[2][3],而dp[2][3]在统计时加上了dp[2][1];

所以未更新的dp[2][5]包涵更新后的dp[2][3]和更新后的dp[2][1];
未更新的dp[2][3]包涵更新后的dp[2][1];
我们需要减去未更新的dp[2][3];

 

所以逆循环,就可以减去未更新的值;

 

这样就ok了(大概全网这题的最详细的题解了)

 

代码

#include<bits/stdc++.h>
#define re register
typedef long long ll;
using namespace std;
inline ll read()
{
    ll a=0,f=1; char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
    while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
    return a*f;
}//好用的快读 
ll n,q;
ll head[200010],dp[200010][21];
struct ljj
{
    ll to,stb;
}a[200010];
ll s=0;
inline void insert(ll x,ll y)
{
    s++;
    a[s].stb=head[x];
    a[s].to=y;
    head[x]=s;
}
inline void dfs(ll x,ll fa)
{
    for(re ll i=head[x];i;i=a[i].stb)
    {
        ll xx=a[i].to;
        if(xx==fa)
            continue;
        dfs(xx,x);
        for(re ll j=1;j<=q;j++)
            dp[x][j]+=dp[xx][j-1];//第一遍dp 
    }
}
inline void dfs1(ll x,ll fa)
{
    for(re ll i=head[x];i;i=a[i].stb)
    {
        ll xx=a[i].to;
        if(xx==fa)
            continue;
        //在第一次遍历时 dp[1][2] 包括了 dp[2][1] 2的子树权值;
        //然鹅  ans在统计dp[2][3] 的时候也加上了 dp[2][1]  2的子树权值;
        //第二次遍历 dp[2][3] 又加上了 dp[2][1];
        //所以需要简单容斥一下; 
        for(re ll j=q;j>=2;j--)
            dp[xx][j]-=dp[xx][j-2];//简单容斥 
        for(re ll j=1;j<=q;j++)
            dp[xx][j]+=dp[x][j-1];//第二遍dp 
        dfs1(xx,x);
    }
}
int main()
{
    n=read();q=read(); 
    for(re ll i=1;i<n;i++)
    {
        ll x=read(),y=read();
        insert(x,y);
        insert(y,x);
    }
    for(re ll i=1;i<=n;i++)
        dp[i][0]=read();//每个节点往外0距离,就是它本身的权值; 
    dfs(1,0);
    dfs1(1,0);
    for(re ll i=1;i<=n;i++)
    {
        ll ans=0;
        for(re ll j=0;j<=q;j++)
            ans+=dp[i][j];//ans统计答案 
        printf("%lld\n",ans);
    }
    return 0;
}

 

posted @ 2020-08-01 14:03  木偶人-怪咖  阅读(195)  评论(2编辑  收藏  举报
*访客位置3D地图 *目录