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.  [Image] 

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 u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ 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:

$2 \rightarrow 1 \rightarrow 3 \rightarrow 7$ and $7 \rightarrow 3 \rightarrow 1 \rightarrow 2$.

However, you should choose the first one as it starts in the city with the lower number.

Difficulty: HARD
Tags: Tree algorithms

Graph algorithms

Graph traversal

Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
from collections import deque
from sys import exit

def parser():
	return [int(x) for x in input().split()]

def DFS_Discriminiting():
	visited = [False for x in range(n)]
	visited[numbers_of_attacked_cities[0]] = True
	stack = []
	intrudoction_order = []
	stack.append(numbers_of_attacked_cities[0])
	while len(stack) > 0:
		v = stack.pop()
		for u in adjacents_list[v]:
			if not visited[u]:
				pi[u] = v
				visited[u] = True
				if attacked_city[u]:
					count_attacked_cities_subtree[u] += 1
				stack.append(u)
				intrudoction_order.append(u)
	for v in intrudoction_order[::-1]:
		count_attacked_cities_subtree[pi[v]] += count_attacked_cities_subtree[v]
		if count_attacked_cities_subtree[v] == 0:
			important_cities[v] = False

def DFS_Heigths():
	visited = [False for x in range(n)]
	visited[numbers_of_attacked_cities[0]] = True
	stack = []
	intrudoction_order = []
	stack.append(numbers_of_attacked_cities[0])
	while len(stack) > 0:
		v = stack.pop()
		for u in adjacents_list[v]:
			if not visited[u]:
				pi[u] = v
				visited[u] = True
				stack.append(u)
				intrudoction_order.append(u)
	for v in intrudoction_order[::-1]:
		if heights1[pi[v]] < heights1[v] + 1:
			heights2[pi[v]] = heights1[pi[v]]
			heights1[pi[v]] = heights1[v] + 1
		elif heights2[pi[v]] < heights1[v] + 1:
			heights2[pi[v]] = heights1[v] + 1

def Distance_Root(s):
	for v in adjacents_list[s]:
		if heights1[v] + 1 > distances1[s]:
			distances2[s] = distances1[s]
			distances1[s] = heights1[v] + 1
		elif heights1[v] + 1 > distances2[s]:
			distances2[s] = heights1[v] + 1

def DFS_Distances():
	visited = [False for x in range(n)]
	visited[numbers_of_attacked_cities[0]] = True
	stack = []
	stack.append(numbers_of_attacked_cities[0])
	Distance_Root(numbers_of_attacked_cities[0])
	while len(stack) > 0:
		v = stack.pop()
		for u in adjacents_list[v]:
			if not visited[u]:
				pi[u] = v
				visited[u] = True
				determinate = False
				stack.append(u)
				if heights1[u] + 1 == distances1[v]:
					if heights1[u] + 1 > distances2[v]:
						determinate = True
						distances1[u] = max(heights1[u], distances2[v] + 1)
						if distances1[u] == heights1[u]:
							distances2[u] = max(distances2[v] + 1, heights2[u])
						else:
							distances2[u] = heights1[u]
				if not determinate:
					distances1[u] = distances1[v] + 1
					distances2[u] = heights1[u]

def BFS(s):
	distance = [-1 for x in range(n)]
	distance[s] = 0
	q = deque()
	q.append(s)
	while len(q) > 0:
		v = q.popleft()
		for u in adjacents_list[v]:
			if distance[u] == -1:
				distance[u] = distance[v] + 1
				q.append(u)
	return distance
(n, m) = parser()
pi = [0 for x in range(n)]
count_attacked_cities_subtree = [0 for x in range(n)]
attacked_city = [False for x in range(n)]
important_cities = [True for x in range(n)]
adjacents_list = [[] for x in range(n)]
for i in range(n - 1):
	edge = parser()
	adjacents_list[edge[0] - 1].append(edge[1] - 1)
	adjacents_list[edge[1] - 1].append(edge[0] - 1)
numbers_of_attacked_cities = []
for i in parser():
	numbers_of_attacked_cities.append(i - 1)
if m == 1:
	print(numbers_of_attacked_cities[0] + 1)
	print(0)
	exit()
for i in numbers_of_attacked_cities:
	attacked_city[i] = True
DFS_Discriminiting()
adjacents_list = [[] for x in range(n)]
count_edges = 0
for v in range(n):
	if v == numbers_of_attacked_cities[0]:
		continue
	elif important_cities[v] and important_cities[pi[v]]:
		adjacents_list[v].append(pi[v])
		adjacents_list[pi[v]].append(v)
		count_edges += 1
pi = [0 for x in range(n)]
heights1 = [0 for x in range(n)]
heights2 = [0 for x in range(n)]
DFS_Heigths()
distances1 = [0 for x in range(n)]
distances2 = [0 for x in range(n)]
DFS_Distances()
lower = distances1[numbers_of_attacked_cities[0]]
for i in range(n):
	if important_cities[i] and lower > distances1[i]:
		lower = distances1[i]
centers = []
for i in range(n):
	if distances1[i] == lower:
		centers.append(i)
posibles_begin_cities = []
for i in centers:
	distances_center = BFS(i)
	max_distance = 0
	for j in range(n):
		if distances_center[j] > max_distance:
			max_distance = distances_center[j]
	for j in range(n):
		if distances_center[j] == max_distance:
			posibles_begin_cities.append(j)
print(min(posibles_begin_cities) + 1)
print(2 * count_edges - (distances1[centers[0]] + distances2[centers[0]]))


from collections import deque
from sys import stdin
from sys import exit

def parser():
	return map(int, stdin.readline().split())

def DFS_Discriminiting():
	visited = [False for x in range(n)]
	visited[numbers_of_attacked_cities[0]] = True
	stack = []
	introduction_order = []
	stack.append(numbers_of_attacked_cities[0])
	while len(stack) > 0:
		v = stack.pop()
		for u in adjacents_list[v]:
			if not visited[u]:
				pi[u] = v
				visited[u] = True
				if attacked_city[u]:
					count_attacked_cities_subtree[u] += 1
				stack.append(u)
				introduction_order.append(u)
	for v in introduction_order[::-1]:
		count_attacked_cities_subtree[pi[v]] += count_attacked_cities_subtree[v]
		if count_attacked_cities_subtree[v] == 0:
			important_cities[v] = False

def DFS_Heigths():
	visited = [False for x in range(n)]
	visited[numbers_of_attacked_cities[0]] = True
	stack = []
	introduction_order = []
	stack.append(numbers_of_attacked_cities[0])
	while len(stack) > 0:
		v = stack.pop()
		for u in adjacents_list[v]:
			if not visited[u]:
				visited[u] = True
				stack.append(u)
				introduction_order.append(u)
	for v in introduction_order[::-1]:
		if heights1[pi[v]] < heights1[v] + 1:
			heights2[pi[v]] = heights1[pi[v]]
			heights1[pi[v]] = heights1[v] + 1
		elif heights2[pi[v]] < heights1[v] + 1:
			heights2[pi[v]] = heights1[v] + 1

def Distance_Root(s):
	for v in adjacents_list[s]:
		if heights1[v] + 1 > distances1[s]:
			distances2[s] = distances1[s]
			distances1[s] = heights1[v] + 1
		elif heights1[v] + 1 > distances2[s]:
			distances2[s] = heights1[v] + 1

def DFS_Distances():
	visited = [False for x in range(n)]
	visited[numbers_of_attacked_cities[0]] = True
	stack = []
	stack.append(numbers_of_attacked_cities[0])
	Distance_Root(numbers_of_attacked_cities[0])
	while len(stack) > 0:
		v = stack.pop()
		for u in adjacents_list[v]:
			if not visited[u]:
				visited[u] = True
				stack.append(u)
				determinate = False
				if heights1[u] + 1 == distances1[v]:
					if heights1[u] + 1 > distances2[v]:
						determinate = True
						distances1[u] = max(heights1[u], distances2[v] + 1)
						if distances1[u] == heights1[u]:
							distances2[u] = max(distances2[v] + 1, heights2[u])
						else:
							distances2[u] = heights1[u]
				if not determinate:
					distances1[u] = distances1[v] + 1
					distances2[u] = heights1[u]

def BFS(s):
	distance = [-1 for x in range(n)]
	distance[s] = 0
	q = deque()
	q.append(s)
	while len(q) > 0:
		v = q.popleft()
		for u in adjacents_list[v]:
			if distance[u] == -1:
				distance[u] = distance[v] + 1
				q.append(u)
	return distance
(n, m) = parser()
pi = [0 for x in range(n)]
count_attacked_cities_subtree = [0 for x in range(n)]
attacked_city = [False for x in range(n)]
important_cities = [True for x in range(n)]
adjacents_list = [[] for x in range(n)]
for i in range(n - 1):
	(v1, v2) = parser()
	adjacents_list[v1 - 1].append(v2 - 1)
	adjacents_list[v2 - 1].append(v1 - 1)
numbers_of_attacked_cities = [x - 1 for x in parser()]
if m == 1:
	print(numbers_of_attacked_cities[0] + 1)
	print(0)
	exit()
for i in numbers_of_attacked_cities:
	attacked_city[i] = True
DFS_Discriminiting()
adjacents_list = [[] for x in range(n)]
count_edges = 0
for v in range(n):
	if v == numbers_of_attacked_cities[0]:
		continue
	elif important_cities[v] and important_cities[pi[v]]:
		adjacents_list[v].append(pi[v])
		adjacents_list[pi[v]].append(v)
		count_edges += 1
heights1 = [0 for x in range(n)]
heights2 = [0 for x in range(n)]
DFS_Heigths()
distances1 = [0 for x in range(n)]
distances2 = [0 for x in range(n)]
DFS_Distances()
min_distance = distances1[numbers_of_attacked_cities[0]]
for i in range(n):
	if important_cities[i] and min_distance > distances1[i]:
		min_distance = distances1[i]
center = []
for i in range(n):
	if distances1[i] == min_distance:
		center.append(i)
posibles_begin_cities = []
for i in center:
	distances_center = BFS(i)
	max_distance = 0
	for j in range(n):
		if distances_center[j] > max_distance:
			max_distance = distances_center[j]
	for j in range(n):
		if distances_center[j] == max_distance:
			posibles_begin_cities.append(j)
print(min(posibles_begin_cities) + 1)
print(2 * count_edges - (distances1[center[0]] + distances2[center[0]]))


from collections import deque
(n, m) = [int(x) for x in input().split()]
adj = [[] for x in range(n + 1)]
for _ in range(1, n):
	(a, b) = [int(x) for x in input().split()]
	adj[a].append(b)
	adj[b].append(a)
chaos = [int(x) for x in input().split()]
s = chaos[0]
chaos = set(chaos)
cc = [0] * (n + 1)
st = deque()
st.append((s, -1))
while len(st):
	(u, e) = st.pop()
	if u < 0:
		if e >= 0:
			cc[e] += cc[-u]
		continue
	if u in chaos:
		cc[u] += 1
	st.append((-u, e))
	for v in adj[u]:
		if v != e:
			st.append((v, u))
adj = [list(filter(lambda v: cc[v] > 0, u)) for u in adj]
a = (s, 0)
st = deque()
st.append((a[0], -1, 0))
while len(st):
	(u, e, h) = st.pop()
	if h > a[1]:
		a = (u, h)
	elif h == a[1] and u < a[0]:
		a = (u, h)
	for v in adj[u]:
		if v != e:
			st.append((v, u, h + 1))
b = a
a = (a[0], 0)
st = deque()
st.append((a[0], -1, 0))
while len(st):
	(u, e, h) = st.pop()
	if h > a[1]:
		a = (u, h)
	elif h == a[1] and u < a[0]:
		a = (u, h)
	for v in adj[u]:
		if v != e:
			st.append((v, u, h + 1))
print(min(a[0], b[0]))
print(2 * (n - cc.count(0)) - a[1])


from heapq import *
INF = float('inf')
(n, m) = list(map(int, input().split()))
adj = [[] for _ in range(n + 1)]
wg = ng = [0 for _ in range(n + 1)]
for _ in range(n - 1):
	(a, b) = list(map(int, input().split()))
	adj[a].append(b)
	adj[b].append(a)
aaa = set(map(int, input().split()))
if len(aaa) == 1:
	print(min(aaa))
	print(0)
	return
rm = []
for i in range(n + 1):
	ng[i] = len(adj[i])
	if i not in aaa and ng[i] == 1:
		rm.append(i)
for a in aaa:
	ng[a] = 0

def remove_node(index):
	while adj[index]:
		nx = adj[index].pop()
		adj[nx].remove(index)
		ng[nx] -= 1
		if ng[nx] == 1:
			rm.append(nx)
	ng[index] = 0
while rm:
	remove_node(rm.pop())
state = [0 for _ in range(n + 1)]
que = [(min(aaa), None)]
res = 0
for _ in range(2):
	deep = [0 for _ in range(n + 1)]
	while que:
		res += 1
		(root, proot) = que.pop()
		for nx in adj[root]:
			if proot == nx:
				continue
			if _:
				state[nx] = root
			deep[nx] = deep[root] + 1
			que.append((nx, root))
	if _:
		break
	start = max(1, deep.index(max(deep)))
	que = [(start, None)]
end = max(1, deep.index(max(deep)))
i = end
path = 1
while i != start:
	path += 1
	i = state[i]
print(min(start, end))
print(res - 1 - path)


def f(k, h):
	print(k, h)
	exit()

def g(p):
	i = p.pop()
	return (i, t[i])
import sys
d = list(map(int, sys.stdin.read().split()))
(n, m) = (d[0], d[1])
if n == 1:
	f(1, 0)
k = 2 * n
t = [[0, set(), i, 0] for i in range(n + 1)]
for i in d[k:]:
	t[i][0] = 1
for (a, b) in zip(d[2:k:2], d[3:k:2]):
	t[a][1].add(b)
	t[b][1].add(a)
p = ([], [])
for x in t:
	if len(x[1]) == 1:
		p[x[0]].append(x[2])
k = s = 1
while p[0]:
	(i, x) = g(p[0])
	(j, y) = g(x[1])
	y[1].remove(i)
	if len(y[1]) == 1:
		p[y[0]].append(j)
if len(p[k]) == 1:
	f(p[k][0], 0)
for i in p[k]:
	t[i][3] = 1
while 1:
	s += 2
	(i, x) = g(p[k])
	(j, y) = g(x[1])
	if len(y[1]) == 1:
		f(min(x[2], y[2]), s - x[3] - y[3])
	x[3] += 1
	if x[3] > y[3]:
		y[2:] = x[2:]
	elif x[3] == y[3]:
		y[2] = min(x[2], y[2])
	y[1].remove(i)
	if len(y[1]) == 1:
		p[1 - k].append(j)
	if not p[k]:
		k = 1 - k


import sys
d = list(map(int, sys.stdin.read().split()))
(n, m) = (d[0], d[1])
k = 2 * n

class T:

	def __init__(t, i):
		t.p = set()
		t.h = t.d = 0
		t.k = i

def g(k, h):
	print(k, h)
	exit()
t = [T(i) for i in range(n + 1)]
for i in d[k:]:
	t[i].d = 1
for (a, b) in zip(d[2:k:2], d[3:k:2]):
	t[a].p.add(t[b])
	t[b].p.add(t[a])
u = [x for x in t if len(x.p) == 1]
v = []
while u:
	x = u.pop()
	if x.d:
		v.append(x)
	elif x.p:
		y = x.p.pop()
		y.p.remove(x)
		if len(y.p) == 1:
			u.append(y)
if len(v) == 1:
	g(v[0].k, 0)
if len(t) == 2:
	g(t[1].k, 0)
for x in v:
	x.h = 1
s = 1
while 1:
	s += 2
	x = v.pop()
	y = x.p.pop()
	if len(y.p) == 1 and x in y.p:
		g(min(x.k, y.k), s - x.h - y.h)
	h = x.h + 1
	if h > y.h:
		(y.h, y.k) = (h, x.k)
	elif h == y.h:
		y.k = min(x.k, y.k)
	y.p.remove(x)
	if len(y.p) == 1:
		u.append(y)
	if not v:
		(v, u) = (u, v)