time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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.

【题目链接】:http://codeforces.com/contest/592/problem/D

【题解】



把包含所有这m个节点的最小生成树所包含的总点数cnt搞出来;

把这个子树从原树中隔离出来:

则从这个子树中的任意一个节点开始遍历;直到遍历完所有的节点再回来;总共遍历的边数为(cnt-1)*2,即这些边要走过去又回来;

但我们不必要走回来了;所以需要减去从终点回到起点的这么一个距离t(遍历完就不用回来了!)

显然这个t最大的时候走过的边数最少;

最长链!

求最长链的方法是;从这个处理出的子树上的任意一个节点开始进行dfs;走到离这个节点最远的点;作为新的起点s1;

再从s1开始处理出距离s1最远的节点s2;

则s1到s2的距离就是最长链的距离;

在树中我们可以把距离看成是树的深度;

最后用(cnt-1)*2减去这个最长链的距离就是答案了;

至于开始的点,只要从这个最长链的两个端点中选一个序号小的就可以了;



【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long using namespace std; const int MAXN = 150000;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0); int n,m,dep[MAXN];
vector <int> a[MAXN];
bool mark[MAXN],in[MAXN]; void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void dfs(int x,int fa)
{
dep[x] = dep[fa]+1;
int len = a[x].size();
if (mark[x])
in[x] = true;
for (int i = 0;i <= len-1;i++)
{
int y = a[x][i];
if (y==fa)
continue;
dfs(y,x);
in[x] |= in[y];
}
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(m);
for (int i = 1;i <= n-1;i++)
{
int x,y;
rei(x);rei(y);
a[x].push_back(y);
a[y].push_back(x);
}
for (int i = 1;i <= m;i++)
{
int x;
rei(x);
mark[x] = true;
}
for (int i = 1;i <= n;i++)
if (mark[i])
{
dfs(i,0);
break;
}
int qi,des=0,cnt=0;
for (int i = 1;i <= n;i++)
if (in[i])
{
cnt++;
if (mark[i])
{
if (dep[i]>des)
{
des = dep[i];
qi = i;
}
}
}
int ans = (cnt-1)*2;
memset(dep,0,sizeof(dep));
dfs(qi,0);
int z,des2=0;
for (int i = 1;i <= n;i++)
if (mark[i] && dep[i]>des2)
{
des2 = dep[i];
z = i;
}
printf("%d\n",min(z,qi));
printf("%d\n",ans-(des2-1));
return 0;
}

【27.66%】【codeforces 592D】Super M的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【39.66%】【codeforces 740C】Alyona and mex

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【27.40%】【codeforces 599D】Spongebob and Squares

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 750C】New Year and Rating(做法2)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【21.21%】【codeforces round 382D】Taxes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. [Angular] Update FormArray with patchValue

    Currently, patchValue doesn't support update FormArray. The workarround is you need to empty the for ...

  2. Android多线程研究(6)——多线程之间数据隔离

    在上一篇<Android多线程研究(5)--线程之间共享数据>中对线程之间的数据共享进行了学习和研究,这一篇我们来看看怎样解决多个线程之间的数据隔离问题,什么是数据隔离呢?比方说我们如今开 ...

  3. (笑话)切,我也是混血儿,我爸是A型血,我妈是B型血!

    1.中午,在家里看电视,电视里正在说起食品安全问题.侄儿突然感叹道:“现在的食品真不让人放心啊!”嘿,没想到侄儿小小年纪竟有这般认识,我正要抓住机会教育他不要乱吃零食.这时侄儿幽怨的瞪着我说:“我昨晚 ...

  4. js的AJAX请求有关知识总结

    什么是AJAX?AJAX作用是什么? async javascript and xml(异步的javascript和xml) 作用:实现局部刷新 async:我们真实项目中一般使用AJAX从服务器端获 ...

  5. 【习题 3-10 UVA - 1587】Box

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举某个顶角的三个相邻面就好. 看看这三个相邻面有没有对应的面. 以及3个相邻面的6个边. 能否分成2个a,2个b,2个c 也即每个 ...

  6. BP神经网络公式推导及实现(MNIST)

    BP神经网络的基础介绍见:http://blog.csdn.net/fengbingchun/article/details/50274471,这里主要以公式推导为主. BP神经网络又称为误差反向传播 ...

  7. POJ 2479 Maximum sum POJ 2593 Max Sequence

    d(A) = max{sum(a[s1]..a[t1]) + sum(a[s2]..a[t2]) | 1<=s1<=t1<s2<=t2<=n} 即求两个子序列和的和的最大 ...

  8. hadoop的关键进程 分类: A1_HADOOP 2015-06-06 11:37 52人阅读 评论(0) 收藏

    hadoop集群中主要进程有 master:   NameNode, ResourceManager, slaves:   DataNode, NodeManager,  RunJar, MRAppM ...

  9. Injector Job深入分析 分类: H3_NUTCH 2015-03-10 15:44 334人阅读 评论(0) 收藏

    Injector Job的主要功能是根据crawlId在hbase中创建一个表,将将文本中的seed注入表中. (一)命令执行 1.运行命令 [jediael@master local]$ bin/n ...

  10. angular之Http服务

    原文 https://www.jianshu.com/p/53e4a4bfad7d 大纲 1.什么是angular服务 2.服务的类别 3.认识angular的Http请求 4.简单实例 5.angu ...