问题描述

BZOJ2591

LG3047


题解

换根树形DP。

设 \(opt[i][j]\) 代表 当 \(1\) 为根时,\(i\) 为根的子树中,到 \(i\) 的距离为 \(j\) 的权值和

此时我们就可以得到 \(1\) 号结点的答案。

考虑这样做 \(n\) 遍,可以求出答案,但是会T飞掉。

观察每次暴力DP,发现大部分结点的信息还是相同的,这是优化复杂度的关键所在。

考虑换根。

从 \(x\) 号结点转移到 \(y\) 号节点上,发现只有 \(x,y\) 两个结点的信息被改变了。

换根后

只要将 \(y\) 结点距离 \(p\) 加上 \(x\) 结点距离 \(p-1\) 的信息就行了。

但是发现 \(x\) 号结点距离 \(p-1\) 的信息中,还包含 \(y\) 号结点 \(p-2\) 的信息,所以要倒序枚举 \(p\) ,去重。


\(\mathrm{Code}\)

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. template <typename Tp>
  4. void read(Tp &x){
  5. x=0;char ch=1;int fh;
  6. while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
  7. if(ch=='-') ch=getchar(),fh=-1;
  8. else fh=1;
  9. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
  10. x*=fh;
  11. }
  12. const int maxn=100007;
  13. const int maxm=200007;
  14. int n,k;
  15. int Head[maxn],to[maxm],Next[maxm],tot;
  16. int c[maxn];
  17. void add(int x,int y){
  18. to[++tot]=y,Next[tot]=Head[x],Head[x]=tot;
  19. }
  20. int opt[maxn][21];
  21. void dp(int x,int f){
  22. opt[x][0]=c[x];
  23. for(int i=Head[x];i;i=Next[i]){
  24. int y=to[i];
  25. if(y==f) continue;
  26. dp(y,x);
  27. for(int j=1;j<=k;j++){
  28. opt[x][j]+=opt[y][j-1];
  29. }
  30. }
  31. }
  32. int ans[maxn];
  33. void calc(int x,int y){
  34. for(int i=k;i>=2;i--) opt[y][i]+=opt[x][i-1]-opt[y][i-2];
  35. opt[y][1]+=opt[x][0];
  36. }
  37. void zy(int x,int f){
  38. for(int i=0;i<=k;i++) ans[x]+=opt[x][i];
  39. for(int i=Head[x];i;i=Next[i]){
  40. int y=to[i];
  41. if(y==f) continue;
  42. calc(x,y);zy(y,x);
  43. }
  44. }
  45. int main(){
  46. read(n);read(k);
  47. for(int i=1,x,y;i<n;i++){
  48. read(x);read(y);
  49. add(x,y);add(y,x);
  50. }
  51. for(int i=1;i<=n;i++) read(c[i]);
  52. dp(1,0);zy(1,0);
  53. for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
  54. return 0;
  55. }

BZOJ2591/LG3047 「USACO12FEB」Nearby Cows 换根树形DP的更多相关文章

  1. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  2. POJ3585:Accumulation Degree(换根树形dp)

    Accumulation Degree Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3425   Accepted: 85 ...

  3. [BZOJ3566][SHOI2014]概率充电器 换根树形DP

    链接 题意:n个充电元件形成一棵树,每个点和每条边都有各自的充电概率,元件可以自身充电或者通过其他点和边间接充电,求充电状态元件的期望个数 题解 设1为根节点 设 \(f[x]\) 表示 \(x\) ...

  4. loj2542 「PKUWC2018」随机游走 【树形dp + 状压dp + 数学】

    题目链接 loj2542 题解 设\(f[i][S]\)表示从\(i\)节点出发,走完\(S\)集合中的点的期望步数 记\(de[i]\)为\(i\)的度数,\(E\)为边集,我们很容易写出状态转移方 ...

  5. loj#2542. 「PKUWC2018」随机游走(树形dp+Min-Max容斥)

    传送门 首先,关于\(Min-Max\)容斥 设\(S\)为一个点的集合,每个点的权值为走到这个点的期望时间,则\(Max(S)\)即为走遍这个集合所有点的期望时间,\(Min(S)\)即为第一次走到 ...

  6. 51nod1812树的双直径(换根树DP)

    传送门:http://www.51nod.com/Challenge/Problem.html#!#problemId=1812 题解:头一次写换根树DP. 求两条不相交的直径乘积最大,所以可以这样考 ...

  7. 【bzoj2591】[Usaco 2012 Feb]Nearby Cows 树形dp

    题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...

  8. 【LibreOJ】#6395. 「THUPC2018」城市地铁规划 / City 背包DP+Prufer序

    [题目]#6395. 「THUPC2018」城市地铁规划 / City [题意]给定n个点要求构造一棵树,每个点的价值是一个关于点度的k次多项式,系数均为给定的\(a_0,...a_k\),求最大价值 ...

  9. 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp

    「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...

随机推荐

  1. How to: Use Both Entity Framework and XPO in a Single Application 如何:在单个应用程序中同时使用实体框架和 XPO

    This topic demonstrates how to create a simple XAF application that uses both the Entity Framework ( ...

  2. python yield关键词使用总结

    python yield关键词使用总结 by:授客 QQ:1033553122 测试环境 win10 python 3.5 yield功能简介 简单来说,yield 的作用就是把一个函数变成一个 ge ...

  3. AlertDialog创建对话框的测试

    AlertDialog的功能是非常强大的,它可以创建各种对话框,它的结构分为:图标区.标题区.内容区.按钮区共四个区域.以这样的思路区创建AlertDialog是非常简单的. 创建AlertDialo ...

  4. 3、nio中的selector使用

    通过编写一个客户端和服务器端的例子来熟悉selector的使用 服务端逻辑: 1. 绑定一个端口号2. channel注册到selector中3. 用死循环来监听如果有时间发生,遍历selection ...

  5. [20191127]探究等待事件的本源4.txt

    [20191127]探究等待事件的本源4.txt --//昨天使用ash_wait_chains.sql脚本把各个生产库执行1遍,才发现我对一套系统性能理解错误.--//我一直以为这套系统存储有点问题 ...

  6. diango url的命名和反向解析

    url的命名和反向解析 静态路由 url(r'^login/', views.login,name='login'), 反向解析ht 模板 {% url 'login' %} --> '/app ...

  7. Data Guard:Oracle 12c –新增和更新的功能 (Doc ID 1558256.1)

    Data Guard: Oracle 12c – New and updated Features (Doc ID 1558256.1) APPLIES TO: Oracle Database - E ...

  8. Data Guard Physical Standby - RAC Primary to RAC Standby 使用第二个网络 (Doc ID 1349977.1)

    Data Guard Physical Standby - RAC Primary to RAC Standby using a second network (Doc ID 1349977.1) A ...

  9. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】

    [CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...

  10. form表单中的button自动刷新页面问题

    form表单中如果存在button的话,有可能会出现一个问题:点击button,触发了页面的自动刷新事件. 原因是因为<button>标签默认的类型是submit,即默认的button点击 ...