题意:

在一棵树中,可以从根节点往其他节点加一条边,使得根节点到其他所有节点的距离和最小,输出最小的距离和。

思路:

我们考虑在加的一条边为$1 \to v$,那么在树上从$1 \to v$的路径上,如果有一个点$y$到$v$比到$1$更近,那么这个点$y$的子树里的所有

点都到$v$更近。那么我们找到离根最近的点$y$,那么$y$子树中的所有点都是到$v$更近。

我们考虑:

$f[u]$表示如果添加了$1 \to u$这条边的最小距离和是多少。

$g[u]$表示如果添加了$1 \to u$这条边有多少点到$u$的距离比到根的距离更小。

$sze[u]$表示$u$的子树的大小。

那么对于它的一个儿子$v$,$f[v] = f[u] - 2 \cdot sze[v] + g[u]$。

因为原来到$u$更优的,那么到$v$至少不会比到根更差,但是$v$的子树中的贡献要重新算。

然后更新一下儿子节点的$g[u]$就好了,这个时候到$v$和到根一样优的点就被删去了。

 #include <bits/stdc++.h>
using namespace std; #define ll long long
#define N 200010
#define INFLL 0x3f3f3f3f3f3f3f3f
#define DEG 20
int n;
vector <int> G[N]; int dep[N], fa[DEG][N], sze[N], k[N];
void DFS1(int u)
{
k[u] = (dep[u]) / - ;
for (int i = ; i < DEG; ++i)
fa[i][u] = fa[i - ][fa[i - ][u]];
sze[u] = ;
for (auto v : G[u]) if (v != fa[][u])
{
fa[][v] = u;
dep[v] = dep[u] + ;
DFS1(v); sze[u] += sze[v];
}
} int findkth(int u, int k)
{
for (int i = DEG - ; i >= ; --i)
if ((k >> i) & )
u = fa[i][u];
return u;
} int f[N]; ll g[N], res;
void DFS2(int u)
{
if (u != )
{
if (dep[u] <= )
{
f[u] = sze[u];
g[u] = g[] - 1ll * (dep[u] - ) * sze[u];
}
else
{
int pre = fa[][u];
g[u] = g[pre] - * sze[u] + f[pre];
f[u] = sze[findkth(u, k[u])];
}
}
res = min(res, g[u]);
for (auto v : G[u]) if (v != fa[][u])
DFS2(v);
} int main()
{
int T; cin >> T;
while (T--)
{
scanf("%d", &n);
for (int i = ; i <= n; ++i) G[i].clear();
memset(sze, , sizeof sze);
for (int i = , u, v; i < n; ++i)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dep[] = ; DFS1();
res = INFLL;
g[] = ;
for (int i = ; i <= n; ++i)
g[] += dep[i];
DFS2();
printf("%lld\n", res);
}
return ;
}

ZOJ 3949 Edge to the Root的更多相关文章

  1. 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 ...

  2. ZOJ 3949 Edge to the Root( 树形dp)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 题解:树dp真的很直觉,或者说dp真的很直觉.就上周末比赛时其实前一 ...

  3. ZOJ 3949 Edge to the Root(树形DP)

    [题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你 ...

  4. 树dp...吧 ZOJ 3949

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...

  5. POJ 3100 &amp; ZOJ 2818 &amp; HDU 2740 Root of the Problem(数学)

    题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...

  6. ZOJ 3949 (17th 浙大校赛 B题,树型DP)

    题目链接  The 17th Zhejiang University Programming Contest Problem B 题意  给定一棵树,现在要加一条连接$1$(根结点)和$x$的边,求加 ...

  7. ZOJ 2048(Prim 或者 Kruskal)

    Highways Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge The island nation of F ...

  8. imx6 gpio irq

    /***************************************************************** * gpio irq * * 一直以来都没了解过gpio的irq, ...

  9. 【BZOJ-3697&3127】采药人的路径&YinandYang 点分治 + 乱搞

    3697: 采药人的路径 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 681  Solved: 246[Submit][Status][Discus ...

随机推荐

  1. zabbix高级玩法之通过xml文件一次性导入

    author:headsen  chen date: 2018-11-10  10:08:38 背景:zabbix的硬件监控架构已经部署完成,接下来就是创建主机这一步,2400台主机如果采用一台台的创 ...

  2. 【咸鱼教程】TextureMerger1.6.6 二:Sprite Sheet的制作和使用

    Sprite Sheet主要用于将零碎的小图合并成一张整图.减少加载图片时http的请求次数. 1 打开TextureMerger,选择Sprite Sheet 2  添加纹理(未创建项目时,会先弹出 ...

  3. U盘安装Centos7.1操作系统的问题记录

    需要的软硬件环境>>>>>>>>>>>>>>>>>1.服务器(笔者用的笔记本).U盘2.Cento ...

  4. 【CF896E】Welcome home, Chtholly 暴力+分块+链表

    [CF896E]Welcome home, Chtholly 题意:一个长度为n的序列ai,让你支持两种操作: 1.l r x:将[l,r]中ai>x的ai都减去x.2.l r x:询问[l,r ...

  5. Android 获取View中的组件

    可以把这个view强转成ViewGroup对象,再通过getChildAt(0),getChildAt(1) 获取之后AddView可能会报错:IllegalStateException: The s ...

  6. MVC + ajaxform 文件上传

    一.前端cshtml代码 <tr> <td width="130" align="right">添加附件:</td> @us ...

  7. Elasticsearch 索引、更新、删除文档

    一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...

  8. saltstack-----上线环境篇(一)

    在11上安装mysql yum install mariadb mariadb-server -y 在mysql的配置文件my.cnf中加入 init_connect='SET collation_c ...

  9. python----并发之协程

    <python并发之协程>一: 单线程下实现并发,即只在一个主线程,并且cpu只有一个的情况下实现并发.(并发的本质:切换+保存状态) cpu正在运行一个任务,会在两种情况下切去执行其他的 ...

  10. SQL Fundamentals || Single-Row Functions || 数字函数number functions

    SQL Fundamentals || Oracle SQL语言 SQL Fundamentals: Using Single-Row Functions to Customize Output使用单 ...