/** * author: zjs * created: 19.08.2026 00:43:04 **/ #include #include // does not include cassert since GCC 16. using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif const int maxn = 2e5 + 5; const int LOG = 18; int anc[maxn][LOG]; vector g[maxn]; int depth[maxn]; int sz[maxn]; void dfs(int u, int p) { sz[u] = 1; depth[u] = depth[p] + 1; anc[u][0] = p; for (int i = 1; i < LOG; i++) anc[u][i] = anc[anc[u][i - 1]][i - 1]; for (int v : g[u]) if (v != p) { dfs(v, u); sz[u] += sz[v]; } } int lca(int u, int v) { if (depth[u] < depth[v]) swap(u, v); int diff = depth[u] - depth[v]; for (int i = 0; i < LOG; i++) if (diff >> i & 1) u = anc[u][i]; if (u == v) return u; for (int i = LOG - 1; i >= 0; i--) { if (anc[u][i] != anc[v][i]) { u = anc[u][i]; v = anc[v][i]; } } return anc[u][0]; } int kth_anc(int u, int k) { for (int i = 0; i < LOG; i++) if (k >> i & 1) u = anc[u][i]; return u; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1, 0); while (q--) { int s, t; cin >> s >> t; if ((depth[s] + depth[t]) & 1) { cout << 0 << '\n'; continue; } int LCA = lca(s, t); int d = depth[s] + depth[t] - 2 * depth[LCA]; if (depth[s] < depth[t]) swap(s, t); int ans; if (depth[s] == depth[t]) { int A = kth_anc(s, d / 2 - 1); int B = kth_anc(t, d / 2 - 1); ans = n - sz[A] - sz[B]; } else { int M = kth_anc(s, d / 2); int A = kth_anc(s, d / 2 - 1); ans = sz[M] - sz[A]; } cout << ans << '\n'; } }