INF = 1001001001 N, K = map(int, input().split()) adj = [[] for _ in range(N)] for _ in range(N - 1): u, v = map(int, input().split()) u -= 1; v -= 1 adj[u].append(v) adj[v].append(u) Ds = list(map(int, input().split())) for i in range(K): Ds[i] -= 1 in_D = [False] * N for D in Ds: in_D[D] = True # 最小シュタイナー木みたいなのを作る。 stack = [] edge_Steiner = [] for l in range(N): if len(adj[l]) <= 1: if in_D[l]: edge_Steiner.append(l) else: stack.append(l) reserved = [True] * N while stack: v = stack.pop() assert not in_D[v] reserved[v] = False for nv in adj[v]: if reserved[nv]: if in_D[nv]: edge_Steiner.append(nv) else: stack.append(nv) # 各頂点から、 # 最小シュタイナー木みたいなのにたどりつくための距離と、 # たどりつく先の頂点を求める。 dist_Steiner = [INF] * N nearest = [-1] * N for v in range(N): if reserved[v]: dist_Steiner[v] = 0 nearest[v] = v stack = edge_Steiner.copy() while stack: v = stack.pop() for nv in adj[v]: if dist_Steiner[nv] > dist_Steiner[v] + 1: dist_Steiner[nv] = dist_Steiner[v] + 1 nearest[nv] = nearest[v] stack.append(nv) # シュタイナー木を一周するときに、帰りの分をさぼれる。 # そのさぼった分の最大化をする。 def dist_from_root_in_reserved(root): dist_diameter = [INF] * N dist_diameter[root] = 0 stack = [root] while stack: v = stack.pop() for nv in adj[v]: if reserved[nv] and dist_diameter[nv] > dist_diameter[v] + 1: dist_diameter[nv] = dist_diameter[v] + 1 stack.append(nv) for i in range(N): if dist_diameter[i] == INF: dist_diameter[i] = -1 return dist_diameter dist_0 = dist_from_root_in_reserved(edge_Steiner[0]) end1 = max(range(N), key = lambda i: dist_0[i]) dist_end1 = dist_from_root_in_reserved(end1) end2 = max(range(N), key = lambda i: dist_end1[i]) dist_end2 = dist_from_root_in_reserved(end2) max_dist = [max(e1, e2) for e1, e2 in zip(dist_end1, dist_end2)] # print(max_dist) # print(dist_0) # print(dist_end1) # print(dist_end2) loop_len = (sum(reserved) - 1) * 2 for v in range(N): s = nearest[v] answer = dist_Steiner[v] + loop_len - max_dist[s] print(answer)