#include using namespace std; const int LOG = 17; struct lowest_common_ancestor{ vector d; vector> pp; lowest_common_ancestor(vector &p, vector &d): d(d){ int N = p.size(); pp = vector>(LOG, vector(N, -1)); pp[0] = p; for (int i = 0; i < LOG - 1; i++){ for (int j = 0; j < N; j++){ if (pp[i][j] != -1){ pp[i + 1][j] = pp[i][pp[i][j]]; } } } } int la(int v, int x){ for (int i = 0; i < LOG; i++){ if ((x >> i & 1) == 1){ v = pp[i][v]; } } return v; } int lca(int u, int v){ if (d[u] > d[v]){ swap(u, v); } v = la(v, d[v] - d[u]); if (u == v){ return u; } for (int i = LOG - 1; i >= 0; i--){ if (pp[i][u] != pp[i][v]){ u = pp[i][u]; v = pp[i][v]; } } return pp[0][u]; } }; int main(){ int N, Q; cin >> N >> Q; 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 p(N, -1); vector> c(N); vector d(N, 0); 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); d[w] = d[v] + 1; q.push(w); } } } reverse(bfs.begin(), bfs.end()); vector dp(N, 1); for (int v : bfs){ for (int w : c[v]){ dp[v] += dp[w]; } } lowest_common_ancestor T(p, d); for (int i = 0; i < Q; i++){ int A, B; cin >> A >> B; A--; B--; if (d[A] > d[B]){ swap(A, B); } if (d[A] % 2 != d[B] % 2){ cout << 0 << endl; } else if (d[A] == d[B]){ int C = T.lca(A, B); if (d[A] == d[B]){ int A2 = T.la(A, d[A] - d[C] - 1); int B2 = T.la(B, d[B] - d[C] - 1); cout << N - dp[A2] - dp[B2] << endl; } else { int D = d[A] + d[B] - d[C] * 2; int X = T.la(B, D / 2); int Y = T.la(B, D / 2 - 1); cout << dp[X] - dp[Y] << endl; } } } }