#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; struct EulerTour { std::vector tour, left, right, down, up, depth; EulerTour(const std::vector> &graph, int root = 0) : graph(graph) { int n = graph.size(); left.resize(n); right.resize(n); down.assign(n, -1); up.assign(n, (n - 1) << 1); int idx = 0; dfs(-1, root, 0, idx); } template void v_update(int ver, Fn f) const { f(left[ver], right[ver] + 1); } template T v_query(int ver, Fn f) const { return f(left[ver], right[ver] + 1); } template T e_query(int u, int v, Fn f) const { return f(down[u] + 1, down[v] + 1); } template void subtree_e_update(int ver, Fn f) const { f(down[ver] + 1, up[ver]); } template T subtree_e_query(int ver, Fn f) const { return f(down[ver] + 1, up[ver]); } private: const std::vector> graph; void dfs(int par, int ver, int now_depth, int &idx) { left[ver] = tour.size(); tour.emplace_back(ver); depth.emplace_back(now_depth); for (int e : graph[ver]) { if (e != par) { down[e] = idx++; dfs(ver, e, now_depth + 1, idx); tour.emplace_back(ver); depth.emplace_back(now_depth); up[e] = idx++; } } right[ver] = tour.size() - 1; } }; template struct SparseTable { using Fn = std::function; SparseTable() {} SparseTable(const std::vector &a, Fn fn) { init(a, fn); } void init(const std::vector &a, Fn fn_) { is_built = true; fn = fn_; int n = a.size(), table_h = 0; lg.assign(n + 1, 0); for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; while ((1 << table_h) <= n) ++table_h; dat.assign(table_h, std::vector(n)); for (int j = 0; j < n; ++j) dat[0][j] = a[j]; for (int i = 1; i < table_h; ++i) for (int j = 0; j + (1 << i) <= n; ++j) { dat[i][j] = fn(dat[i - 1][j], dat[i - 1][j + (1 << (i - 1))]); } } Band query(int left, int right) const { assert(is_built && left < right); int h = lg[right - left]; return fn(dat[h][left], dat[h][right - (1 << h)]); } private: bool is_built = false; Fn fn; std::vector lg; std::vector> dat; }; struct LowestCommonAncestor : EulerTour { LowestCommonAncestor(const std::vector> &graph, int root = 0) : EulerTour(graph, root) { int n = this->tour.size(); std::vector

nodes(n); for (int i = 0; i < n; ++i) nodes[i] = {this->depth[i], this->tour[i]}; st.init(nodes, [](P a, P b) -> P { return std::min(a, b); }); } int query(int u, int v) const { u = this->left[u]; v = this->left[v]; if (u > v) std::swap(u, v); return st.query(u, v + 1).second; } private: using P = std::pair; SparseTable

st; }; struct HeavyLightDecomposition { std::vector parent, subtree, id, inv, head; HeavyLightDecomposition(const std::vector> &graph, int root = 0) : graph(graph) { int n = graph.size(); parent.assign(n, -1); subtree.assign(n, 1); id.resize(n); inv.resize(n); head.resize(n); dfs1(root); head[root] = root; int now_id = 0; dfs2(root, now_id); } template void v_update(int u, int v, Fn f) const { while (true) { if (id[u] > id[v]) std::swap(u, v); f(std::max(id[head[v]], id[u]), id[v] + 1); if (head[u] == head[v]) return; v = parent[head[v]]; } } template T v_query(int u, int v, F f, G g, const T ID) const { T left = ID, right = ID; while (true) { if (id[u] > id[v]) { std::swap(u, v); std::swap(left, right); } left = g(left, f(std::max(id[head[v]], id[u]), id[v] + 1)); if (head[u] == head[v]) break; v = parent[head[v]]; } return g(left, right); } template void subtree_v_update(int ver, Fn f) const { f(id[ver], id[ver] + subtree[ver]); } template T subtree_v_query(int ver, Fn f) const { return f(id[ver], id[ver] + subtree[ver]); } template void e_update(int u, int v, Fn f) const { while (true) { if (id[u] > id[v]) std::swap(u, v); if (head[u] == head[v]) { f(id[u], id[v]); break; } else { f(id[head[v]] - 1, id[v]); v = parent[head[v]]; } } } template T e_query(int u, int v, F f, G g, const T ID) const { T left = ID, right = ID; while (true) { if (id[u] > id[v]) { std::swap(u, v); std::swap(left, right); } if (head[u] == head[v]) { left = g(left, f(id[u], id[v])); break; } else { left = g(left, f(id[head[v]] - 1, id[v])); v = parent[head[v]]; } } return g(left, right); } template void subtree_e_update(int ver, Fn f) const { f(id[ver], id[ver] + subtree[ver] - 1); } template T subtree_e_query(int ver, Fn f) const { return f(id[ver], id[ver] + subtree[ver] - 1); } int lowest_common_ancestor(int u, int v) const { while (true) { if (id[u] > id[v]) std::swap(u, v); if (head[u] == head[v]) return u; v = parent[head[v]]; } } private: std::vector> graph; void dfs1(int ver) { for (int &e : graph[ver]) { if (e != parent[ver]) { parent[e] = ver; dfs1(e); subtree[ver] += subtree[e]; if (subtree[e] > subtree[graph[ver].front()]) std::swap(e, graph[ver].front()); } } } void dfs2(int ver, int &now_id) { id[ver] = now_id++; inv[id[ver]] = ver; for (int e : graph[ver]) { if (e != parent[ver]) { head[e] = (e == graph[ver].front() ? head[ver] : e); dfs2(e, now_id); } } } }; template struct FenwickTreeSupportingRangeAddQuery { FenwickTreeSupportingRangeAddQuery(int n_, const Abelian ID = 0) : n(n_), ID(ID) { ++n; dat_const.assign(n, ID); dat_linear.assign(n, ID); } void add(int left, int right, Abelian val) { if (right < ++left) return; for (int i = left; i < n; i += i & -i) { dat_const[i] -= val * (left - 1); dat_linear[i] += val; } for (int i = right + 1; i < n; i += i & -i) { dat_const[i] += val * right; dat_linear[i] -= val; } } Abelian sum(int idx) const { Abelian res = ID; for (int i = idx; i > 0; i -= i & -i) res += dat_linear[i]; res *= idx; for (int i = idx; i > 0; i -= i & -i) res += dat_const[i]; return res; } Abelian sum(int left, int right) const { return left < right ? sum(right) - sum(left) : ID; } Abelian operator[](const int idx) const { return sum(idx, idx + 1); } private: int n; const Abelian ID; std::vector dat_const, dat_linear; }; int main() { int n, k; cin >> n >> k; vector> graph(n); REP(_, n - 1) { int u, v; cin >> u >> v; --u; --v; graph[u].emplace_back(v); graph[v].emplace_back(u); } vector d(k); REP(i, k) cin >> d[i], --d[i]; LowestCommonAncestor lca(graph, d.front()); sort(ALL(d), [&](int a, int b) -> bool { return lca.left[a] < lca.right[b]; }); ll steiner = 0; REP(i, k) steiner += lca.depth[d[i]] + lca.depth[d[(i + 1) % k]] - lca.depth[lca.query(d[i], d[(i + 1) % k])] * 2; HeavyLightDecomposition hld(graph, d.front()); FenwickTreeSupportingRangeAddQuery bit(n); REP(i, k) hld.v_update(d[i], d[(i + 1) % k], [&](int u, int v) -> void { bit.add(u, v, 1); }); vector dist(n, -1), from(n, -1); queue que; REP(i, n) { if (bit[i] > 0) { dist[i] = 0; from[i] = i; que.emplace(i); } } while (!que.empty()) { int ver = que.front(); que.pop(); for (int e : graph[ver]) { if (dist[e] == -1) { dist[e] = dist[ver] + 1; from[e] = from[ver]; que.emplace(e); } } } vector has_acorn(n, false); REP(i, k) has_acorn[d[i]] = true; vector>> child(n); auto f = [&](auto&& f, int par, int ver) -> void { for (int e : graph[ver]) { if (e == par) continue; f(f, ver, e); if (!child[e].empty()) { child[ver].emplace_back(child[e].front().first + 1, e); } else if (has_acorn[e]) { child[ver].emplace_back(1, e); } } sort(ALL(child[ver]), greater{}); }; f(f, -1, d.front()); auto g = [&](auto&& g, int par, int ver, int pd) -> void { if (par != -1) { child[ver].emplace_back(pd, par); sort(ALL(child[ver]), greater{}); } for (int e : graph[ver]) { if (e == par) continue; if (child[ver].empty()) { assert(has_acorn[ver]); g(g, ver, e, 1); } else if (child[ver].front().second == e) { if (child[ver].size() == 1) { assert(has_acorn[ver]); g(g, ver, e, 1); } else { g(g, ver, e, child[ver][1].first + 1); } } else { g(g, ver, e, child[ver].front().first + 1); } } }; g(g, -1, d.front(), 0); REP(i, n) cout << steiner + dist[i] - (child[from[i]].empty() ? 0 : child[from[i]].front().first) << '\n'; return 0; }