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

Input
7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7
Output
2
3
Input
6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6
Output
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(虚树+树的直径)的更多相关文章

  1. Codeforces 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...

  2. CodeForces - 592D Super M 题解

    题目大意: 一棵树 n个点 有m个点被标记 求经过所有被标记的点的最短路径的长度以及起点(如有多条输出编号最小的起点). 思路: 1.当且仅当一个点本身或其子树中有点被标记时该点在最短的路径上因此,可 ...

  3. CodeForces 592D Super M DP

    Super M 题解: 定义 dp[u][0] 为遍历完u中的所有节点, 但不回到u点的路径花费值. 定义 dp[u][1] 为遍历完u中的所有节点, 且要回到u点的路径花费值. 转移方程. dp[u ...

  4. CodeForces 592D Super M

    先把没用的边去掉,求出包含m个点的最小树.然后求出最小树的直径就可以得到答案了. #include <cstdio> #include <cstring> #include & ...

  5. 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分

    树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...

  6. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  7. Codeforces 633C Spy Syndrome 2 | Trie树裸题

    Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...

  8. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  9. <虚树+树型DP> HNOI2014世界树

    <虚树+树型DP> HNOI2014世界树 #include <iostream> #include <cstdio> #include <cstring&g ...

随机推荐

  1. Linux 编译ffmpeg 生成ffplay

    本来主要介绍linux环境下如何编译ffmpeg使之生成ffplay.编译总是离不开源码的版本,以及编译环境下:编译环境Ubutun 16.04 ,ffmpeg 版本3.4.2.如何下载ffmpeg ...

  2. SpringMVC请求流程与原理分析

    SpringMVC的工作原理图: SpringMVC流程 1.  用户发送请求至前端控制器DispatcherServlet. 2.  DispatcherServlet收到请求调用HandlerMa ...

  3. GitHub使用问题(遇到一个记一个)

    1.如何创建文件夹: 如图,Create new files,点击后,若需要创建文件,输入文件名即可,但如果创建的是文件夹,需要在文件夹名后 加一个 '/'斜杠,然后就变成文件夹了

  4. ui-router $transitions 用法

    1. //route redirection $transitions.onStart({to: 'manage'}, function (trans) { var params = trans.pa ...

  5. Cocoapods完整使用篇

    温馨提示:在篇文章中所使用的Xcode版本为Xcode7.   一.什么是CocoaPods? 简单来说,就是专门为iOS工程提供对第三方库的依赖的管理工具,通过CocoaPods,我们可以单独管理每 ...

  6. Selenium3 Python3 Web自动化测试从基础到项目实战之二浏览器的不同设置

    在前面一个章节我们知道了如何通过webdriver去初始化我们得driver,然后我们只需要通过driver就能够去做我们得自动化,首先我们知道我们需要知道得是当我们有driver之后,我们剩下得就是 ...

  7. 语法之知识点的改进(Func/Action)

    上一章我们讲到关于面向对象思想上C#和JAVA之差别.笔者分别从面向对象的三大特性入手.而本章主要讲一些C#改进的知识点.在.NET Framework 2.0之后出现很多新的知识点.这些知识点更是让 ...

  8. 更精炼更专注的RTMPClient客户端EasyRTMPClient,满足直播、转发、分析等各种需求

    现状 EasyRTMPClient,熟悉的朋友就会联想到EasyRTSPClient项目(https://github.com/EasyDSS/EasyRTSPClient),EasyRTSPClie ...

  9. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:通过接口获取实时信息

    对于动态网站,要实时更新网站的信息,通过接口来获取实时信息是一个必不可少的部分.EasyNVR可以接入IPC等前端设备,必须要实时获取到对应的IPC实时信息进行展示. 本篇主要说明Ajax来获取数据. ...

  10. EasyDarwin支持GB28181协议开发

    本文转自:http://blog.csdn.net/gavin1010/article/details/77926853 EasyGB28181服务器开发 背景 当前的安防行业,除了私有协议,普遍使用 ...