#include using namespace std; struct heavy_light_decomposition{ vector p, d, sz, in, next; void dfs1(vector> &c, int v = 0){ sz[v] = 1; for (int &w : c[v]){ dfs1(c, w); sz[v] += sz[w]; d[w] = d[v] + 1; if (sz[w] > sz[c[v][0]]){ swap(w, c[v][0]); } } } void dfs2(vector> &c, int &t, int v = 0){ in[v] = t; t++; for (int w : c[v]){ if (w == c[v][0]){ next[w] = next[v]; } else { next[w] = w; } dfs2(c, t ,w); } } heavy_light_decomposition(vector &p, vector> &c): p(p){ int N = p.size(); sz = vector(N, 0); d = vector(N, 0); dfs1(c); in = vector(N, 0); next = vector(N, 0); int t = 0; dfs2(c, t); } int lca(int u, int v){ while (true){ if (in[u] > in[v]){ swap(u, v); } if (next[u] == next[v]){ return u; } v = p[next[v]]; } } int dist(int u, int v){ return d[u] + d[v] - d[lca(u, v)] * 2; } }; int main(){ int N, K; cin >> N >> K; vector> E(N); for (int i = 0; i < N - 1; i++){ int u, v; cin >> u >> v; u--; v--; E[u].push_back(v); E[v].push_back(u); } vector D(K); for (int i = 0; i < K; i++){ cin >> D[i]; D[i]--; } vector p(N, -1); vector> c(N); queue Q; Q.push(0); vector bfs; while (!Q.empty()){ int v = Q.front(); Q.pop(); bfs.push_back(v); for (int w : E[v]){ if (w != p[v]){ p[w] = v; c[v].push_back(w); Q.push(w); } } } heavy_light_decomposition T(p, c); vector> P(K); for (int i = 0; i < K; i++){ P[i] = make_pair(T.in[D[i]], D[i]); } sort(P.begin(), P.end()); int S = 0; vector imos(N, 0); for (int i = 0; i < K; i++){ int u = P[i].second; int v = P[(i + 1) % K].second; S += T.dist(u, v); imos[u]++; imos[v]++; int w = T.lca(u, v); imos[w]--; if (w > 0){ imos[p[w]]--; } } reverse(bfs.begin(), bfs.end()); for (int v : bfs){ if (v != 0){ imos[p[v]] += imos[v]; } } vector d1(K); for (int i = 0; i < K; i++){ d1[i] = T.dist(0, D[i]); } int s = max_element(d1.begin(), d1.end()) - d1.begin(); vector d2(K); for (int i = 0; i < K; i++){ d2[i] = T.dist(D[s], D[i]); } int t = max_element(d2.begin(), d2.end()) - d2.begin(); vector ans(N, -1); for (int i = 0; i < N; i++){ if (imos[i] > 0){ ans[i] = S - max(T.dist(D[s], i), T.dist(D[t], i)); } } queue Q2; for (int i = 0; i < N; i++){ if (imos[i] > 0){ Q2.push(i); } } while (!Q2.empty()){ int v = Q2.front(); Q2.pop(); for (int w : E[v]){ if (ans[w] == -1){ ans[w] = ans[v] + 1; Q2.push(w); } } } for (int i = 0; i < N; i++){ cout << ans[i] << endl; } }