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. 洛谷—— P1434 滑雪

    https://www.luogu.org/problem/show?pid=1434#sub 题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜 ...

  2. Android中Application类的详解:

    Android中Application类的详解: 我们在平时的开发中,有时候可能会须要一些全局数据.来让应用中的全部Activity和View都能訪问到.大家在遇到这样的情况时,可能首先会想到自定义一 ...

  3. Altium Designer如何删除以布的线

  4. JS数据类型的转换规则

    数据类型转换的规则 1 如果只有一个值,判断这个值是真还是假,遵循只有0,NaN,'',null,undefined这五个是假的,其余的都是真 2 如果是两个值比较是否相等,遵循以下规则: ![]-& ...

  5. HDU 2473 Junk-Mail Filter 并查集删除(FZU 2155盟国)

    http://acm.hdu.edu.cn/showproblem.php?pid=2473 http://acm.fzu.edu.cn/problem.php?pid=2155 题目大意: 编号0~ ...

  6. (转)Windows Server 2012 R2虚拟机自激活(AVMA)技术

    转自: 老丁的技术博客 相信Hyper-v管理员都有这样的经历,安装多台虚拟机后,都要一台一台手工激活,如果虚拟机足够多的话,这是一项很繁琐的工作,但从Windows Server 2012 R2开始 ...

  7. [Angular2 Router] Redirects and Path Matching - Avoid Common Routing Pitfall

    In this tutorial we are going to learn how we can can configure redirects in the angular 2 router co ...

  8. express 学习笔记(一)路由

    先导入express: var express = require('express'); var app = express(); 1.路由方法: get, post, put, head, del ...

  9. stm32的串口中断

    下面有很多问题没有验证: 在设置USART_CR1中的TE位时,会发送一个空闲帧作为第一次数据发送, 目前我所了解的串口中断发送,有两种方式: 一个是:TC 一个是:TXE 这是判断两个标志位, 第一 ...

  10. POJ 2406 Power Strings KMP求周期

    传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...