[BZOJ2591][Usaco 2012 Feb]Nearby Cows

摘要:
FJ想知道每个草原上奶牛Mi的最大可能数量,即通过K条道路后可能达到i的奶牛总数。仔细看$kle20$,所以这个问题非常有趣。将$f[i][j]$设置为以$i$为根的子树,并将$j$移动到刚好到达$i$中的牛数。显然,$f[u][j]=sumf[son][j-1]$,dfs是可以的,但这将错过来自父节点的牛。然后,我们设置$g[i][j]$来指示整棵树上走$j$刚好够到$i$的牛的数量。然后对每个节点$u$再次进行dfs,$g[u][j]=f[u][j]+g[fa][j-1]-g[u][j-2]$最后一个$i$答案是$sum_{0lejlek}g[i][j]$#include<cstdio˃#includecharbuf[10000000],*ptr=buf-1;内联线程(){intn=0;while;whilen=++;returnn;}组分maxn=100000+10,maxk=20+5;structEdge{intto,next;Edge(){}Edge:to,next{}}e[maxn*2];intfir[maxn]={0},cnt=0;inlinetins{e[++cnt]=边缘;fir[u]=cnt;e[++-cnt]=边缘intn,k;intw[maxn];intf[maxn][maxk]={0},g[maxn][maxk];voiddfs1{//forf[u][i]=0;f[u][0]=w[u];for{v=e[i].to;ifcontinue;dfs1(v,u);forf[u][j]+=f[v][j-1];}forg[u][i]=f[u][i];}voiddfs2{对于{v=e[i].to;ifcontinue;g[v][1]+=w[u];forg[v][j]+=g[u][j-1]-f[v][j-2];dfs2(v,u);}}intmain(){buf[frad]=0;n=readint();k=readint(;forins;forw[i]=readint(();dfs1(1,0);dfs2(1,0;for{sum=0;forsum+=g[i][j];printf;}return0;}

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
 
一开始以为$kle n$吓尿了。。。
仔细看$kle 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_{0le jle 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
", sum);
    }
    return 0;
}
 

免责声明:文章转载自《[BZOJ2591][Usaco 2012 Feb]Nearby Cows》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇WPF学习笔记:ComboBox的数据绑定Springboot 日志管理配置logback-spring.xml下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

centos安装、卸载openssh

1.卸载openssh并执行rpm-qaopenssh*以检查是否已安装。...

如何开发一款浏览器[转]

另一个问题是“开发浏览器有什么困难?”,范围不限于PC或移动浏览器。从这个角度来看,开发浏览器并不容易。有很多种类的知识和困难需要处理,但如此多的努力将得到相应的回报。InfoQ的读者们,您是否也考虑过开发浏览器?你对如何开发浏览器有什么看法?...

如何控制el-image预览图片的大小

Src=“scope.row.carlouseUrl”:1。从“element-ui/packages/image/src/image-viewer”2导入图像查看器importerImageViewer。寄存器组件:3。使用组件&lt;El table columnlabel=“旋转图表”width=“220px”&gt;...

汇编指令MOV

格式:MOVDST,SRC例如:MOVEAX,#050aH;将十六进制050a传送到通用寄存器eax中MOVDI,BXMOVES,AXMOVAX,DSMOVAL,23HMOV[2000H],02HMOV[2061H],BX...

5G中的频点计算及实例分析

相关图表:关于∏SSB的频域位置SSREF和GSCN之间的关系,请参见下表:注:SCSspacedchannelrasterisM=3的工作频带的默认值。同步网格是5G的第一个概念,旨在加快终端扫描SSB的频率位置。GSCN通常用于在SA联网模式下加速时频同步,以继续解释MIB和SIB1消息;对于NSA来说,这是不必要的。RRC重配置消息已经携带了NR的SS...

AcWing算法基础课

第二行包含n个整数,表示整数序列。输出格式对于每个输出指令PM,输出表示当前集合中最小值的结果。每个结果占据一行。数据范围1≤ N≤ 105109≤ x(x)≤ 109是合法的。输入示例:8I-10PMI-10D1C28I6PMDM输出示例:-106分析:对于向上和向下操作,请注意ph和hp的应用,这相当于指针ph[k]=t;插入堆中的Kth的位置为t;hp...