ZOJ 3949 Edge to the Root
题意:
在一棵树中,可以从根节点往其他节点加一条边,使得根节点到其他所有节点的距离和最小,输出最小的距离和。
思路:
我们考虑在加的一条边为$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的更多相关文章
- 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( 树形dp)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 题解:树dp真的很直觉,或者说dp真的很直觉.就上周末比赛时其实前一 ...
- ZOJ 3949 Edge to the Root(树形DP)
[题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你 ...
- 树dp...吧 ZOJ 3949
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Secon ...
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...
- ZOJ 3949 (17th 浙大校赛 B题,树型DP)
题目链接 The 17th Zhejiang University Programming Contest Problem B 题意 给定一棵树,现在要加一条连接$1$(根结点)和$x$的边,求加 ...
- ZOJ 2048(Prim 或者 Kruskal)
Highways Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge The island nation of F ...
- imx6 gpio irq
/***************************************************************** * gpio irq * * 一直以来都没了解过gpio的irq, ...
- 【BZOJ-3697&3127】采药人的路径&YinandYang 点分治 + 乱搞
3697: 采药人的路径 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 681 Solved: 246[Submit][Status][Discus ...
随机推荐
- Promise在await报错后,如何继续往下跑...
一.resolve 当a>0时,正常情况依次输出A.B.C console.log("A"); let result = await this.test(); console ...
- 使用iLO远程管理HP系列服务器
iLO是Integrated Ligths-out的简称,是HP服务器上集成的远程管理端口,它是一组芯片内部集成vxworks嵌入式操作系统,通过一个标准RJ45接口连接到工作环境的交换机.只要将服务 ...
- python获取windows所有com口
import serial import serial.tools.list_ports port_list = list(serial.tools.list_ports.comports()) po ...
- Oracle备份恢复之断电导致控制文件和日志文件损坏修复
Oracle数据库遭遇断电遭遇ora-00214.ora-00314.ora-00312错误恢复案例一枚 1.数据库在17日21:19启动开始报错ora-214错误: Tue Jan 17 21:19 ...
- textField placeholder颜色,位置设置
自定义textField继承自UITextField 重写 - (CGRect)placeholderRectForBounds:(CGRect)bounds _phoneTF.font = HPFo ...
- 线性参照,M值的相关测试
怎样使用普通线要素获取带M值的线要素 怎样查看线要素各个折点上的M值,怎样导出为一张表 线性参照:http://resources.arcgis.com/zh-cn/help/main/10.2/in ...
- centos7 安装composer
命令: 1.下载composer wget https://dl.laravel-china.org/composer.phar -O /usr/local/bin/composer 2.赋予权限 c ...
- 1128 - Greatest Parent---LightOj(LCA+离线算法)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1128 给你一颗树,树的每个节点都有一个权值,树根是节点0,权值为1,树中每个节点的权值 ...
- Java非静态内部类为什么不能有静态成员
我们可以把InnerClass看成OuterClass的非静态成员,它的初始化必须在外部类对象创建后以后进行,要加载InnerClass必须在实例化OuterClass之后完成 ,java虚拟机要求所 ...
- java获取系统当前服务器IP地址
public String getServiceIp(){ InetAddress address; String myIp; try { address = InetAddress.getLocal ...