ZOJ 3949 Edge to the Root(树形DP)
【题目链接】 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949
【题目大意】
给出一棵根为1的树,每条边边长为1,请你从根连一条边到某个点,
使得各点到根距离的总和最小,求这个最小距离和
【题解】
假设从1连到x,那么受影响的只有1和x中点往下的部分,
我们发现中点往下的部分根据每个点答案改变的大小可以分为多个部分,
每个部分大小为其1-x链上的父节点子树大小减去其链上第一子节点子树大小,
设其链上父节点为y,那么这一块里每个点的距离变化为2*dy-1-dx
那么答案变化为∑(2*dy-1-dx)=-sizeall*dx+∑(2*dy-1)
所以我们按链维护(1-2*dy)*sizey的前缀和,这里sizey指的是固定块的大小而不是子树大小
固定块的大小我们可以通过子树大小加减得到,当计算另一条链的时候,我们回溯消除影响,
对于节点x的答案,我们可以取其链上前缀和与其中点的链上前缀和的差值,
就得到了受影响部分的(1-2*dy)*sizey的和,在加上sizeall*dx,得到答案变化
我们求出每个节点答案可能变化的最小值,和原本的固定答案相加,就是这题的答案。
【代码】
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=200010;
vector<int> G[N];
int cnt,st[N],size[N],d[N];
LL C[N],S[N],ans,all;
void dfs(int x,int fx){
d[x]=d[fx]+1;
all+=d[x];
size[x]=1;
for(int i=0;i<G[x].size();i++){
int v=G[x][i];
if(v!=fx){
dfs(v,x);
size[x]+=size[v];
}
}
}
void dfs2(int x,int fx){
if(cnt)S[cnt]-=C[cnt]*size[x];
st[++cnt]=x;
C[cnt]=1-2*d[x];
S[cnt]=S[cnt-1]+C[cnt]*size[x];
if(fx){
int mid=(cnt+1)/2;
ans=min(ans,S[cnt]-S[mid]+1LL*size[st[mid+1]]*d[x]);
}
for(int i=0;i<G[x].size();i++){
int v=G[x][i];
if(v!=fx)dfs2(v,x);
}if(--cnt)S[cnt]+=C[cnt]*size[x];
}
int T,n;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++)G[i].clear();
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}d[all=ans=0]=-1;
dfs(1,0);
dfs2(1,0);
printf("%lld\n",all+ans);
}return 0;
}
ZOJ 3949 Edge to the Root(树形DP)的更多相关文章
- ZOJ 3949 Edge to the Root( 树形dp)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 题解:树dp真的很直觉,或者说dp真的很直觉.就上周末比赛时其实前一 ...
- ZOJ 3949 Edge to the Root(想法)(倍增)
Edge to the Root Time Limit: 1 Second Memory Limit: 131072 KB Given a tree with n vertices, we ...
- ZOJ 3949 Edge to the Root
题意: 在一棵树中,可以从根节点往其他节点加一条边,使得根节点到其他所有节点的距离和最小,输出最小的距离和. 思路: 我们考虑在加的一条边为$1 \to v$,那么在树上从$1 \to v$的路径上, ...
- ZOJ 3626 Treasure Hunt I(树形dp)
Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous country since ...
- ZOJ 3626 Treasure Hunt I (树形DP,常规)
题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...
- ZOJ 3805 (树形DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...
- ZOJ 3201 树形dp+背包(简单题)
#include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...
- ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...
- 树dp...吧 ZOJ 3949
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...
随机推荐
- string 类型转换
string转int "; int n = atoi(str.c_str()); cout << n << endl; int转string #include < ...
- nm用法小记
nm用于显示目标文件的符号,也是二进制工具集(info binutils)里的一员 先来看一个例子,源码和对应的命令结果 四部分分别表示的意义 符号所在的obj文件名 符号的值,这里应该是指符号所在段 ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D
D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- [hdu 3068] Manacher算法O(n)最长回文子串
一个不错的讲解:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.05.md # ...
- HLPP
LOJ 最大流加强版 #include <bits/stdc++.h> const int inf=0x7fffffff; const int maxn=1210; const int m ...
- html与地图
html:<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta ...
- [USACO1.3]虫洞
Luogu链接 题目描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点. 根据他的计算,约翰知道他 ...
- Bzoj3441 乌鸦喝水
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 258 Solved: 97 Description [题目背景] 一只乌鸦在自娱自乐,它在面 ...
- django中管理程序2
升级版 from os import path TASKS_ROOT = path.dirname(path.abspath(path.dirname(__file__))) PYTHON_ROOT ...
- Java坦克大战 (三) 之可完全控制坦克朝八个方向运动
本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...