题意:

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

思路:

我们考虑在加的一条边为$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. Modelsim SE 仿真 ALTERA FPGA IP

    Modelsim SE 仿真 ALTERA FPGA IP 最近,有几个朋友问过我是不是有新版本的Modelsim altera,其原因是 Qii 升级为新版本的,但是没配套的modelsim,没办法 ...

  2. 使用disavled属性锁定input内容不可以修改后,打印获取不到对应的值

    当我们需要锁定input内容不让修改时,可以使用disabled="disabled"和readonly="readonly", 官方的解释是:disabled ...

  3. iOS - 沙盒机制(SandBox)和获取沙盒路径

    iOSAPP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URLScheme.沙盒里面的文件可以是照片.声音文件.文本 ...

  4. [转]Android MediaPlayer状态机

    翻译Android Reference Manual的MediaPlayer的状态机 对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一个MediaPlayer对象被支持的播放控制操作 ...

  5. 爬虫之requests详解

    requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...

  6. 1.5神经网络可视化显示(matplotlib)

    神经网络训练+可视化显示 #添加隐层的神经网络结构+可视化显示 import tensorflow as tf def add_layer(inputs,in_size,out_size,activa ...

  7. CCCC L2-017. 人以群分 贪心

    https://www.patest.cn/contests/gplt/L2-017 题解:贪心,一点小数学 坑:XJB改下标改错了 #include <iostream> #includ ...

  8. Mac操作技巧

    Command+Option+P+R,重置PRAM的. 官方关于重置PRAM的说明.(有助于电脑提速) 安装新版系统的时候失败,原因是下载的镜像有问题版本不对,具体是中国区暂未更新镜像,下载下来的有问 ...

  9. android call and audio

    mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system ConnCtl *:s android audio (http://blog.cs ...

  10. centos7.2 源码编译安装php7.2.4 apache2.4.37 https证书安装

    一.php7.2.11源码安装 1.下载php7.2.11 wget http://cn2.php.net/downloads.php/php-7.2.11.tar.gz#### 2.安装依赖 yum ...