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 want to add an edge between vertex 1 and vertex x, so that the sum of d(1, v) for all vertices v in the tree is minimized, where d(u, v) is the minimum number of edges needed to pass from vertex u to vertex v. Do you know which vertex x we should choose?
Recall that a tree is an undirected connected graph with n vertices and n - 1 edges.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 2 × 105), indicating the number of vertices in the tree.
Each of the following n - 1 lines contains two integers u and v (1 ≤ u, v ≤ n), indicating that there is an edge between vertex u and v in the tree.
It is guaranteed that the given graph is a tree, and the sum of n over all test cases does not exceed 5 × 105. As the stack space of the online judge system is not very large, the maximum depth of the input tree is limited to about 3 × 104.
We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.
Output
For each test case, output a single integer indicating the minimum sum of d(1, v) for all vertices v in the tree (NOT the vertex x you choose).
Sample Input
2
6
1 2
2 3
3 4
3 5
3 6
3
1 2
2 3
Sample Output
8
2
Hint
For the first test case, if we choose x = 3, we will have
d(1, 1) + d(1, 2) + d(1, 3) + d(1, 4) + d(1, 5) + d(1, 6) = 0 + 1 + 1 + 2 + 2 + 2 = 8
It's easy to prove that this is the smallest sum we can achieve.
【分析】给你一棵树,1节点为根。现在在除1号节点外任选一个节点与1节点连一条边,使得其他所有节点到一号节点的距离之和最小,求这个最小的距离之和。
首先咱想一个暴力的方法。直接枚举每一个节点U,使其与1号节点连边,那么U节点及其子树的距离都会被改变,改变值为dis[u]-1(dis[u]=dep[u]-1).再从1节点到U节点引一条路径,路径上的点及其子树的距离也会被改变,如果该路径上一个节点V的距离改变,那么该节点的所有子树(不包括该路径上的V的儿子节点及其子树)都会被改变,而且改变的差值都是一样的。现在我们就来分析一下哪些点会被更新。记录每一个节点的深度,dep[1]=1.比如dep[u]=6,即1-->2-->3-->4-->5-->6,当加一条边1-->6,则5,6节点及他俩的子树都会被改变,注意这里的路径上的节点都可能有子树,而且受5号节点影响的子树不包括6号节点(前面已说明)。
我们从6节点向上找到最后一个距离会被改变的节点,发现是5节点。而如果在6节点后面再接上7节点呢?可以发现还是5节点,然后再在纸上画几个发现:设向上最后一个被修改的节点为x,则dep[x]=dep[u]/2+2.(U为当前枚举的节点)。再看上边这个例子。5,6节点及其子树将被改变,对于6节点及其子树,改变值为(dis[6]-1)*sz[6],5节点及其子树:(dis[5]-1)*(sz[5]-sz[6]),合并得到 总改变值为2*(sz[5]+sz[6]),推广后,对于当前枚举节点dis是奇数的,差值为(2*(sz[u]+sz[fa[u]]+sz[fa[fa[u]]]...+sz[x]))而且可以推出当前节点dis为偶数时(dep为奇数),改变值为(2*(sz[u]+sz[fa[u]]+sz[fa[fa[u]]]...+sz[x])-sz[x]).号公式出来了,现在问题是怎么找这个x节点。嘻嘻,很简单,倍增记录祖先就行了,然后还得记录子树大小前缀和。
#include <bits/stdc++.h>
#define inf 1000000000
#define met(a,b) memset(a,b,sizeof a)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
const int N = 2e5+;
const int M = 4e2+;
const ll mod = 1e9+;
int n,m,k;
ll ans,sz[N],sum[N],pre;
int fa[N][],dep[N];
vector<int>edg[N];
void dfs1(int u,int f){
sz[u]=;
fa[u][]=f;
dep[u]=dep[f]+;
for(int i=;i<;i++){
fa[u][i]=fa[fa[u][i-]][i-];
}
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(v==f)continue;
dfs1(v,u);
sz[u]+=sz[v];
}
pre+=dep[u]-;
}
void dfs2(int u,int f){
sum[u]=sum[f]+sz[u];
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(v==f)continue;
dfs2(v,u);
}
}
int main() {
int op,u,v,x,y;
scanf("%d",&op);
while(op--){
pre=;
met(fa,);
for(int i=;i<N;i++){
edg[i].clear();
sum[i]=;
}
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
edg[u].pb(v);
edg[v].pb(u);
}
dep[]=;
dfs1(,);
dfs2(,);
ans=pre;
for(int i=;i<=n;i++){
int d=dep[i];
int x=d/+;
if(d==||d==)continue;
u=i;
for(int j=;j>=;j--){
if(dep[fa[u][j]]<x)continue;
else if(dep[fa[u][j]]>x)u=fa[u][j];
else {
u=fa[u][j];
break;
}
}
if(d&){
v=fa[u][];
ll ret=*(sum[i]-sum[v])-sz[u];
ans=min(ans,pre-ret);
}
else {
v=fa[u][];
ll ret=*(sum[i]-sum[v]);
ans=min(ans,pre-ret);
}
}
printf("%lld\n",ans);
}
return ;
}
ZOJ 3949 Edge to the Root(想法)(倍增)的更多相关文章
- 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,请你 ...
- ZOJ 3949 Edge to the Root
题意: 在一棵树中,可以从根节点往其他节点加一条边,使得根节点到其他所有节点的距离和最小,输出最小的距离和. 思路: 我们考虑在加的一条边为$1 \to v$,那么在树上从$1 \to v$的路径上, ...
- 树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 ...
- 【Codeforces827D/CF827D】Best Edge Weight(最小生成树性质+倍增/树链剖分+线段树)
题目 Codeforces827D 分析 倍增神题--(感谢T*C神犇给我讲qwq) 这道题需要考虑最小生成树的性质.首先随便求出一棵最小生成树,把树边和非树边分开处理. 首先,对于非树边\((u,v ...
- 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 ...
- [HNOI 2015]开店
Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...
随机推荐
- LightOJ 1321 - Sending Packets 简单最短路+期望
http://www.lightoj.com/volume_showproblem.php?problem=1321 题意:每条边都有概率无法经过,但可以重新尝试,现给出成功率,传输次数和传输时间,求 ...
- .net core 安装Swagger
Install-Package Swashbuckle -Pre 1.Startup // This method gets called by the runtime. Use this metho ...
- 【HDU】3068 最长回文
[算法]manacher [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> us ...
- Response.Redirect在新窗口打开(转载)
Response.Rederect在默认情况下是在本页跳转,所以除了在js中用window.open或是给A标签添加target属性之外,在后台似乎不能来打开新的页面,其实不然,通过设置form的ta ...
- Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)
题目链接:http://codeforces.com/contest/999/problem/E 题目: 题意:给你n个城市,m条单向边,问你需要加多少条边才能使得从首都s出发能到达任意一个城市. 思 ...
- 分布式实时日志分析解决方案ELK部署架构
一.概述 ELK 已经成为目前最流行的集中式日志解决方案,它主要是由Beats.Logstash.Elasticsearch.Kibana等组件组成,来共同完成实时日志的收集,存储,展示等一站式的解决 ...
- HashMap 、LinkedHashMap、HashTable、TreeMap 和 Properties 的区别
HashMap 1.线程不安全: 2.允许null value 和 null key: 3.访问效率比较高: 4.Java1.2引进的Map接口的一个实现: 5.轻量级: 6.根据键的HashCode ...
- rabbitmq之队列性能测试及优化方法(六)
前言 下面关注一下rabbitmq实际使用时的性能问题和怎么进行一些优化. 性能测试 针对每个需要生产/消费者与rabbitmq进行通讯的方法进行测试 测试环境 排除网络IO的干扰,采用生产者和消费者 ...
- 測試 battery capacity curve 的負載
昨天有同事問說, 他要測試 battery capacity curve, 並且負載要使用 33mA, 於是我想到有一個 apk 名稱為 快速放電 (最下方),可以控制 cpu 的 load, 他試了 ...
- 安全测试===sqlmap(零)转载
本文转自:https://blog.werner.wiki/sqlmap-study-notes-0/ 感谢作者的整理,如有侵权,立删 零.前言 这篇文章是我学习Sqlmap的用法时做的笔记,记录了S ...