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. 限制tomcat仅响应本机请求(转)

    http://blog.bbzhh.com/index.php/archives/135.html 在VPS上搭建了nginx和tomcat应用,想通过nginx来反向代理127.0.0.1:8080 ...

  2. crm2013 查看下拉框的选项

    在CRM2011中,我们非常easy查看下拉框的选择.打开页面,按F12.把光标对准目标,就会显示出详细的选项,如图:' watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...

  3. FZU Problem 2062 Suneast & Yayamao

    http://acm.fzu.edu.cn/problem.php?pid=2062 题目大意: 给你一个数n,要求求出用多少个数字可以表示1~n的所有数. 思路: 分解为二进制. 对于一个数n,看它 ...

  4. Day2:购物车小程序

    一.购物车小程序第一版 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan product_list = [ (&quo ...

  5. C# 数据通信

    json asmxwcfwebRequestwebClient 串口 socket

  6. 字符的输入和输出即:getchar和putchar

    #include <stdio.h> int main(int argc, const char * argv[]) { putchar(getchar());//这种方式就是输入一个字符 ...

  7. 24、vb2_buffer和videobuf_buffer比较分析

    看韦东山视频第三期摄像头驱动中构造了自己的vivi驱动,但是使用的videoBuf结构体,新的版本用的是vb2_buffer结构,我机器上(ubuntu12.04)使用的内核是linux3.2,看了看 ...

  8. [AngularJS Ng-redux] Integrate ngRedux

    Up to this point, we have created an effective, yet rudimentary, implementation of Redux by manually ...

  9. 【2013】将x插入有序数列

    Time Limit: 3 second Memory Limit: 2 MB 将一个数x插入到有序数列a中,插入后a仍然有序. Input 第一行输入有序数列a的元素个数 第二行依次输入a的元素,以 ...

  10. [CSS] Conditionally Apply Styles Using Feature Queries @supports

    While browsers do a great job of ignoring styles they don’t understand, it can be useful to provide ...