#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; using vb = vector; using vvb = vector; using vd = vector; using vs = vector; using pii = pair; using pll = pair; using pdd = pair; using vpii = vector; using vpll = vector; using vpdd = vector; const int inf = (1 << 30) - 1; const ll INF = 1LL << 60; //const int MOD = 1000000007; const int MOD = 998244353; using Graph = vector >; struct LCA { vector > parent; // parent[d][v] := 2^d-th parent of v vector depth; LCA() {} LCA(const Graph& G, int r = 0) { init(G, r); } void init(const Graph& G, int r = 0) { int V = (int)G.size(); int h = 1; while ((1 << h) < V) ++h; parent.assign(h, vector(V, -1)); depth.assign(V, -1); dfs(G, r, -1, 0); for (int i = 0; i + 1 < (int)parent.size(); ++i) for (int v = 0; v < V; ++v) if (parent[i][v] != -1) parent[i + 1][v] = parent[i][parent[i][v]]; } void dfs(const Graph& G, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto e : G[v]) if (e != p) dfs(G, e, v, d + 1); } int get(int u, int v) { if (depth[u] > depth[v]) swap(u, v); for (int i = 0; i < (int)parent.size(); ++i) if ((depth[v] - depth[u]) & (1 << i)) v = parent[i][v]; if (u == v) return u; for (int i = (int)parent.size() - 1; i >= 0; --i) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } // 頂点uから深さd遡った頂点の番号 int la(int u, int d) { int j = 0; int p = u; while (d > 0) { if (d & 1) p = parent[j][p]; d = (d >> 1); j++; } return p; } }; void dfs(int v, int p, vi& sz, const vvi& g) { for (auto u : g[v]) { if (u == p) continue; dfs(u, v, sz, g); } sz[v] = 1; for (auto u : g[v]) { sz[v] += sz[u]; } } int main() { int n, q; cin >> n >> q; Graph g(n); for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } vi sz(n); dfs(0, -1, sz, g); LCA lca(g); while (q--) { int s, t; cin >> s >> t; s--; t--; int par = lca.get(s, t); int d1 = lca.depth[s] - lca.depth[par]; int d2 = lca.depth[t] - lca.depth[par]; if (d1 != d2) { if ((d1 + d2) % 2 == 0) { int mid = (d1 + d2) / 2; int v; if (d1 > d2) v = s; else v = t; int pa = lca.la(v, mid); int ch = lca.la(v, mid - 1); cout << sz[pa] - sz[ch] << endl; } else { cout << 0 << endl; } } else { int p1 = lca.la(s, d1 - 1); int p2 = lca.la(t, d2 - 1); cout << n - sz[p1] - sz[p2] << endl; } } return 0; }