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 ...
随机推荐
- python--利用列表推导式快速生成xml格式数据
在接口自动化测试中,我们经常将要发送的数据放到excel里. json数据放至excel方便,但最近的一个测试,数据是xml格式发送的 如下: 属性 必选/可选 描述 1. Message Eleme ...
- css中:hover空格
前面有空格后代所有节点,前面无空格第一个节点 <div class="task-item"> <span><input type="chec ...
- Ucloud云主机无法yum安装处理办法
Ucloud云主机在yum安装的时候出现这个提示 执行一下命令 yum --disablerepo=salttestyum-config-manager --disable salttestyum-c ...
- Git 使用篇二:搭建远程服务器
一般做一个私人的项目,不希望开源的,是不会放在GitHub上的,这个时候我们需要建里一个自己的Git远程服务器,方便小组成员开发. 这里以Centos云服务器为例: 第一步 如果自己的服务器没有git ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 线程池和进程池的通用写法 ProcessPoolExecutor 和 ThreadPoolExecutor
import time from comcurrent.futures import ThreadPoolExecutor,ProcessPoolExccoutor#这个方法可以用进程池或者线程池 d ...
- Java编程思想第四版随书源码官方下载方法
见不少人在找net.mindview.util.Print,CSDN上有下载,收积分,以下是官网的下载方法,免费: 官网链接:http://mindview.net/ 电子书下载地址:http://w ...
- HashMap 的工作原理及代码实现,什么时候用到红黑树
HashMap工作原理及什么时候用到的红黑树: 在jdk 1.7中,HashMap采用位桶+链表实现,即使用链表处理冲突,同一hash值的链表都存储在一个链表里.但是当位于一个桶中的元素较多,即has ...
- UITableView左右滑动cell无法显示“删除”按钮的原因分析
http://www.cocoachina.com/bbs/read.php?tid-145693.html - (void)tableView:(UITableView *)tableView co ...
- JS 防止表单重复提交的方法
第一种:用flag标识,下面的代码设置checkSubmitFlg标志: <script language="”JavaScript”"> var checkSubmi ...