BZOJ2591/LG3047 「USACO12FEB」Nearby Cows 换根树形DP
问题描述
题解
换根树形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}\)
#include<bits/stdc++.h>
using namespace std;
template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
}
const int maxn=100007;
const int maxm=200007;
int n,k;
int Head[maxn],to[maxm],Next[maxm],tot;
int c[maxn];
void add(int x,int y){
to[++tot]=y,Next[tot]=Head[x],Head[x]=tot;
}
int opt[maxn][21];
void dp(int x,int f){
opt[x][0]=c[x];
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(y==f) continue;
dp(y,x);
for(int j=1;j<=k;j++){
opt[x][j]+=opt[y][j-1];
}
}
}
int ans[maxn];
void calc(int x,int y){
for(int i=k;i>=2;i--) opt[y][i]+=opt[x][i-1]-opt[y][i-2];
opt[y][1]+=opt[x][0];
}
void zy(int x,int f){
for(int i=0;i<=k;i++) ans[x]+=opt[x][i];
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(y==f) continue;
calc(x,y);zy(y,x);
}
}
int main(){
read(n);read(k);
for(int i=1,x,y;i<n;i++){
read(x);read(y);
add(x,y);add(y,x);
}
for(int i=1;i<=n;i++) read(c[i]);
dp(1,0);zy(1,0);
for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
return 0;
}
BZOJ2591/LG3047 「USACO12FEB」Nearby Cows 换根树形DP的更多相关文章
- 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 ...
- POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3425 Accepted: 85 ...
- [BZOJ3566][SHOI2014]概率充电器 换根树形DP
链接 题意:n个充电元件形成一棵树,每个点和每条边都有各自的充电概率,元件可以自身充电或者通过其他点和边间接充电,求充电状态元件的期望个数 题解 设1为根节点 设 \(f[x]\) 表示 \(x\) ...
- loj2542 「PKUWC2018」随机游走 【树形dp + 状压dp + 数学】
题目链接 loj2542 题解 设\(f[i][S]\)表示从\(i\)节点出发,走完\(S\)集合中的点的期望步数 记\(de[i]\)为\(i\)的度数,\(E\)为边集,我们很容易写出状态转移方 ...
- loj#2542. 「PKUWC2018」随机游走(树形dp+Min-Max容斥)
传送门 首先,关于\(Min-Max\)容斥 设\(S\)为一个点的集合,每个点的权值为走到这个点的期望时间,则\(Max(S)\)即为走遍这个集合所有点的期望时间,\(Min(S)\)即为第一次走到 ...
- 51nod1812树的双直径(换根树DP)
传送门:http://www.51nod.com/Challenge/Problem.html#!#problemId=1812 题解:头一次写换根树DP. 求两条不相交的直径乘积最大,所以可以这样考 ...
- 【bzoj2591】[Usaco 2012 Feb]Nearby Cows 树形dp
题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...
- 【LibreOJ】#6395. 「THUPC2018」城市地铁规划 / City 背包DP+Prufer序
[题目]#6395. 「THUPC2018」城市地铁规划 / City [题意]给定n个点要求构造一棵树,每个点的价值是一个关于点度的k次多项式,系数均为给定的\(a_0,...a_k\),求最大价值 ...
- 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp
「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...
随机推荐
- 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 ( ...
- python yield关键词使用总结
python yield关键词使用总结 by:授客 QQ:1033553122 测试环境 win10 python 3.5 yield功能简介 简单来说,yield 的作用就是把一个函数变成一个 ge ...
- AlertDialog创建对话框的测试
AlertDialog的功能是非常强大的,它可以创建各种对话框,它的结构分为:图标区.标题区.内容区.按钮区共四个区域.以这样的思路区创建AlertDialog是非常简单的. 创建AlertDialo ...
- 3、nio中的selector使用
通过编写一个客户端和服务器端的例子来熟悉selector的使用 服务端逻辑: 1. 绑定一个端口号2. channel注册到selector中3. 用死循环来监听如果有时间发生,遍历selection ...
- [20191127]探究等待事件的本源4.txt
[20191127]探究等待事件的本源4.txt --//昨天使用ash_wait_chains.sql脚本把各个生产库执行1遍,才发现我对一套系统性能理解错误.--//我一直以为这套系统存储有点问题 ...
- diango url的命名和反向解析
url的命名和反向解析 静态路由 url(r'^login/', views.login,name='login'), 反向解析ht 模板 {% url 'login' %} --> '/app ...
- 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 ...
- 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 ...
- [CodeForces - 1225D]Power Products 【数论】 【分解质因数】
[CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...
- form表单中的button自动刷新页面问题
form表单中如果存在button的话,有可能会出现一个问题:点击button,触发了页面的自动刷新事件. 原因是因为<button>标签默认的类型是submit,即默认的button点击 ...