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. Des 加密cbc模式 padding

    using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...

  2. eclipse 使用jetty调试时,加依赖工程的源码调试方法

    [1] 添加source eclipse-->debug as-->debug configurations-->source [2]若source不起作用 重新编译一下,mvn c ...

  3. 关于python中矩阵的实现和矩阵的转置

    python中矩阵的实现是靠序列,,, 序列有很多形式, 其实矩阵是现实生活中的东西,把现实生活中的结构转换到程序中. 就需要有个实现的方法,而这种路径是多种多样的. 下面给出一个把矩阵转换成pyth ...

  4. 最短路算法详解(Dijkstra/SPFA/Floyd)

    新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...

  5. Flask项目之手机端租房网站的实战开发(一)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一丶项目介绍 产品:关于手机移动端的租房网站 角色:在这个产品中用户包括房东与房客 功能:房东可以在这个平台发布自己的房屋,房客可 ...

  6. Day2:数据类型

    一.数字 1.整型(int),无长整型.python3.x,不论多大的数都是int #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuh ...

  7. Android, IOS 史上最强多语言国际化,不仅第一次会尾随系统,并且会保存用户的语言设置

    劲爆消息,我提供源代码了.你能够先看完再下载.也能够先下载再看完, android源代码地址: https://github.com/hebiao6446/------Bluetooth-Androi ...

  8. docker安装及问题处理

    1.在Ubuntu的命令行中输入 sudo apt-get install docker.io 2.如果切换到了root用户下 apt-get install docker.io 3.对于新安装的Ub ...

  9. 8.6 Android灯光系统_源码分析_背光灯

    Change system screen brightness, using android.provider.Settings.System.SCREEN_BRIGHTNESSandroid-er. ...

  10. [AngularFire 2 ] Hello World - How To Write your First Query using AngularFire 2 List Observables ?

    In this lesson we are going to use AngularFire 2 for the first time. We are going to configure the A ...