首页 技术 正文
技术 2022年11月21日
0 收藏 373 点赞 3,093 浏览 3884 个字

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 longusing 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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,767
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844