CodeForces - 592D: Super M(虚树+树的直径)
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of n cities, connected by n - 1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go from any city to any other city using only the given roads. There are m cities being attacked by humans. So Ari... we meant Super M have to immediately go to each of the cities being attacked to scare those bad humans. Super M can pass from one city to another only using the given roads. Moreover, passing through one road takes her exactly one kron - the time unit used in Byteforces.
However, Super M is not on Byteforces now - she is attending a training camp located in a nearby country Codeforces. Fortunately, there is a special device in Codeforces that allows her to instantly teleport from Codeforces to any city of Byteforces. The way back is too long, so for the purpose of this problem teleportation is used exactly once.
You are to help Super M, by calculating the city in which she should teleport at the beginning in order to end her job in the minimum time (measured in krons). Also, provide her with this time so she can plan her way back to Codeforces.
Input
The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 123456) - the number of cities in Byteforces, and the number of cities being attacked respectively.
Then follow n - 1 lines, describing the road system. Each line contains two city numbers ui and vi (1 ≤ ui, vi ≤ n) - the ends of the road i.
The last line contains m distinct integers - numbers of cities being attacked. These numbers are given in no particular order.
Output
First print the number of the city Super M should teleport to. If there are many possible optimal answers, print the one with the lowest city number.
Then print the minimum possible time needed to scare all humans in cities being attacked, measured in Krons.
Note that the correct answer is always unique.
Examples
7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7
2
3
6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6
2
4
Note
In the first sample, there are two possibilities to finish the Super M's job in 3 krons. They are:
and .
However, you should choose the first one as it starts in the city with the lower number.
题意:给定一棵大小为N的树,然后给定K个特殊点。现在让你在原树上任选一个点起点,然后从这个起点遍历所有的特殊点,走的最短路程是多少,选择的起点是哪个。如果多个起点满足题意,输出编号最小的那一个。
思路:假设必须要走的点组成的树大小为M,那么答案为M*2-dis,其中dis是起点和终点的距离。很显然是在所有必须走到的点中找到树的直径。
如果我们会虚树,那么我们可以很快地求出这棵树:首先把所有特殊点按照DFS序排序,把排序后相邻两点的LCA求出来,把这些LCA耶标记为特殊点,然后除了虚树的根节点,其他特殊点都可以一直向上连边,直到遇到下一个关键点。
(也有其他直接DFS得到这棵树的。假设多次询问,虚树的优点就显现出来了,特殊点连边的时候不需要把之间的非特殊点加进来,而是通过倍增得到距离,然后得到一颗点数不超过2*K个点的树。
#include<bits/stdc++.h>
const int maxn=;
using namespace std;
int Laxt[maxn],Next[maxn],To[maxn],in[maxn],a[maxn],cnt;
int vis[maxn],dep[maxn],fa[maxn][],dis[maxn],S,T,ans,times;
bool cmp(int x,int y) { return in[x]<in[y]; }
void add(int u,int v) { Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; }
void dfs(int u,int f)
{
in[u]=++times; fa[u][]=f; dep[u]=dep[f]+;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=f) dfs(To[i],u);
}
}
void dfs2(int u,int f)
{
dis[u]=dis[f]+;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=f) dfs2(To[i],u);
}
}
int LCA(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);
for(int i=;i>=;i--) if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];
if(u==v) return u;
for(int i=;i>=;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][];
}
int main()
{
int N,M,tot,u,v,i,j;
scanf("%d%d",&N,&M);
for(i=;i<N;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs(,);
for(i=;i<=;i++)
for(j=;j<=N;j++)
fa[j][i]=fa[fa[j][i-]][i-];
for(i=;i<=M;i++) scanf("%d",&a[i]);
sort(a+,a+M+,cmp); tot=M;
for(i=;i<=M;i++) a[++tot]=LCA(a[i-],a[i]);
sort(a+,a+tot+,cmp);
tot=unique(a+,a+tot+)-(a+);
for(i=;i<=tot;i++) vis[a[i]]=; //得到关键点
memset(Laxt,,sizeof(Laxt)); cnt=;
for(i=;i<=tot;i++){ //关键点之间连边得到新树
u=a[i];
while(true){
if(u==a[]) break;
ans++;
add(u,fa[u][]); add(fa[u][],u);
u=fa[u][];
if(vis[u]||u==) break;
}
}
dfs2(a[],); //得到直径
for(i=;i<=tot;i++) if(dis[a[i]]>dis[S]||(dis[a[i]]==dis[S]&&a[i]<S)) S=a[i];
dis[S]=;
dfs2(S,);
for(i=;i<=tot;i++) if(dis[a[i]]>dis[T]||(dis[a[i]]==dis[T]&&a[i]<T)) T=a[i];
printf("%d\n%d\n",min(S,T),ans*-(dis[T]-));
return ;
}
CodeForces - 592D: Super M(虚树+树的直径)的更多相关文章
- Codeforces 592D - Super M - [树的直径][DFS]
Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...
- CodeForces - 592D Super M 题解
题目大意: 一棵树 n个点 有m个点被标记 求经过所有被标记的点的最短路径的长度以及起点(如有多条输出编号最小的起点). 思路: 1.当且仅当一个点本身或其子树中有点被标记时该点在最短的路径上因此,可 ...
- CodeForces 592D Super M DP
Super M 题解: 定义 dp[u][0] 为遍历完u中的所有节点, 但不回到u点的路径花费值. 定义 dp[u][1] 为遍历完u中的所有节点, 且要回到u点的路径花费值. 转移方程. dp[u ...
- CodeForces 592D Super M
先把没用的边去掉,求出包含m个点的最小树.然后求出最小树的直径就可以得到答案了. #include <cstdio> #include <cstring> #include & ...
- 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分
树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces 633C Spy Syndrome 2 | Trie树裸题
Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- <虚树+树型DP> HNOI2014世界树
<虚树+树型DP> HNOI2014世界树 #include <iostream> #include <cstdio> #include <cstring&g ...
随机推荐
- ArcMap中使用ArcPy实现Geometry与WKT的相互转换
在Web GIS迅猛发展的今天,使用浏览器来进行交互以其方便性.快捷性被广大用户所推崇,那么在传输格式方面,都已比較简单的JSON或者WKT来解决网络带宽带来的数据压力. 在ArcGIS10.2版本号 ...
- webStorm 多列编辑
webStorm可以像Sublime一样使用列编辑,只是区别在于webStorm只可以编辑连续列表. 按住alt键鼠标选择一列,然后输入文字就会编辑多行,这个功能很赞,比较实用(按住ALT键选中之后, ...
- iOS开发人员程序许可协议
请细致阅读以下的许可协议条款和条件之前下载或使用苹果软件. 这些条款和条件构成你和苹果之间的法律协议. iOS开发人员程序许可协议 目的 你想使用苹果软件(例如以下定义)来开发一个或多个应 ...
- HDFS源码分析数据块复制之PendingReplicationBlocks
PendingReplicationBlocks实现了所有正在复制的数据块的记账工作.它实现以下三个主要功能: 1.记录此时正在复制的块: 2.一种对复制请求进行跟踪的粗粒度计时器: 3.一个定期识别 ...
- 【WPF学习笔记】之如何把数据库里的值读取出来然后显示在页面上:动画系列之(六)(评论处有学习资料及源码)
(应博友们的需要,在文章评论处有源码链接地址,以及WPF学习资料.工具等,希望对大家有所帮助) ...... 承接系列五 上一节讲了,已经把数据保存到数据库并且删除数据,本讲是把已经存在的数据从数据库 ...
- JAVA使用并行流(ParallelStream)时要注意的一些问题
https://blog.csdn.net/xuxiaoyinliu/article/details/73040808
- Hadoop常见异常及其解决方式
1.Shell$ExitCodeException 现象:执行hadoop job时出现例如以下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : ...
- POJ3182 The Grove[射线法+分层图最短路]
The Grove Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 904 Accepted: 444 Descripti ...
- RTSP转RTMP-HLS网页无插件视频直播-EasyNVR功能介绍-音频开启
EasyNVR简介 EasyNVR能够通过简单的摄像机通道配置.存储配置.云平台对接配置.CDN配置等,将统监控行业里面的高清网络摄像机IP Camera.NVR.移动拍摄设备接入到EasyNVR,E ...
- 使用struts2中默认的拦截器以及自定义拦截器
转自:http://blog.sina.com.cn/s/blog_82f01d350101echs.html 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Acti ...