Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.

The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.

Manao has heard of m settlements affected by the Book of Evil. Their numbers are p_1, p_2, ..., p_{m}. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.


-----Input-----

The first line contains three space-separated integers n, m and d (1 ≤ m ≤ n ≤ 100000; 0 ≤ d ≤ n - 1). The second line contains m distinct space-separated integers p_1, p_2, ..., p_{m} (1 ≤ p_{i} ≤ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers a_{i} and b_{i} representing the ends of this path.


-----Output-----

Print a single number — the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.


-----Examples-----
Input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6

Output
3



-----Note-----

Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.

 [Image]

Source: codeforces | Difficulty: HARD
Tags: Tree algorithms

Dynamic programming

Graph traversal

Divide and conquer


Solutions:
import collections

class Graph:

	def __init__(self, n, dir):
		self.node_cnt = n
		self.__directed = dir
		self.__adjList = []
		for i in range(n):
			self.__adjList.append([])

	def addEdge(self, u, v):
		self.__adjList[u].append(v)
		if not self.__directed:
			self.__adjList[v].append(u)

	def getDistances(self, start, end=None):
		assert 0 <= start and start < self.node_cnt
		dist = [-1] * self.node_cnt
		q = collections.deque()
		dist[start] = 0
		q.append(start)
		while len(q) > 0:
			(z, breakable) = (q.popleft(), False)
			if end == z:
				break
			for t in self.__adjList[z]:
				if dist[t] == -1:
					dist[t] = dist[z] + 1
					q.append(t)
					if t == end:
						breakable = True
						break
			if breakable:
				break
		return dist

def getAffectedDiameter(graph, affected):
	affection = [False for i in range(graph.node_cnt)]
	for x in affected:
		affection[x] = True
	dist0 = graph.getDistances(affected[0])
	affect_1 = -1
	for i in range(n):
		if affection[i] and (affect_1 == -1 or dist0[affect_1] < dist0[i]):
			affect_1 = i
	dist1 = graph.getDistances(affect_1)
	affect_2 = -1
	for i in range(n):
		if affection[i] and (affect_2 == -1 or dist1[affect_2] < dist1[i]):
			affect_2 = i
	return (affect_1, affect_2)
(n, m, d) = map(int, input().split())
p = list(map(lambda s: int(s) - 1, input().split()))
g = Graph(n, dir=False)
for i in range(1, n):
	(a, b) = map(lambda s: int(s) - 1, input().split())
	g.addEdge(a, b)
(p1, p2) = getAffectedDiameter(g, p)
(d1, d2) = (g.getDistances(p1), g.getDistances(p2))
cnt = 0
for i in range(n):
	if d1[i] <= d and d2[i] <= d:
		cnt += 1
print(cnt)


from collections import defaultdict
import sys
input = sys.stdin.readline

class graph:

	def __init__(self, n, mark):
		self.d = defaultdict(list)
		self.n = n
		self.mark = mark

	def add(self, s, d):
		self.d[s].append(d)
		self.d[d].append(s)

	def bfs(self, s, dis):
		marked = s
		visited = [False] * self.n
		visited[s] = True
		q = [s]
		while q:
			s = q.pop(0)
			if s in mark:
				marked = s
			for i in self.d[s]:
				if visited[i] == False:
					q.append(i)
					visited[i] = True
					dis[i] += dis[s] + 1
		return marked
(n, m, k) = list(map(int, input().split()))
mrk = [int(x) for x in input().split()]
mark = {}
for i in mrk:
	mark[i - 1] = 1
g = graph(n, mark)
for i in range(n - 1):
	(a, b) = list(map(int, input().split()))
	g.add(a - 1, b - 1)
dis = [0] * n
u = g.bfs(0, dis)
dis = [0] * n
d = g.bfs(u, dis)
temp = [0] * n
x = g.bfs(d, temp)
count = 0
for i in range(n):
	if temp[i] <= k and dis[i] <= k:
		count += 1
print(count)


import heapq

def dfs(graph, start):
	n = len(graph)
	dist = [-0 for i in range(n + 1)]
	visited = [False for i in range(n + 1)]
	visited[start] = True
	stack = []
	dist[start] = 0
	heapq.heappush(stack, start)
	while stack:
		u = heapq.heappop(stack)
		for v in graph[u]:
			if not visited[v]:
				visited[v] = True
				dist[v] = dist[u] + 1
				heapq.heappush(stack, v)
	return dist

def solution():
	(n, m, d) = list(map(int, input().strip().split()))
	p = list(map(int, input().strip().split()))
	graph = [[] for i in range(n + 1)]
	for i in range(n - 1):
		(a, b) = list(map(int, input().strip().split()))
		graph[a].append(b)
		graph[b].append(a)
	dist = dfs(graph, 1)
	max_distance = -1
	u = -1
	v = -1
	for i in p:
		if dist[i] > max_distance:
			max_distance = dist[i]
			u = i
	distu = dfs(graph, u)
	max_distance = -1
	for i in p:
		if distu[i] > max_distance:
			max_distance = distu[i]
			v = i
	distv = dfs(graph, v)
	affected = 0
	for i in range(1, n + 1):
		if 0 <= distu[i] <= d and 0 <= distv[i] <= d:
			affected += 1
	print(affected)
solution()