/* -*- coding: utf-8 -*- * * 2337.cc: No.2337 Equidistant - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int BN = 18; /* typedef */ typedef vector vi; /* global variables */ vi nbrs[MAX_N]; int ps[MAX_N][BN], ds[MAX_N], ss[MAX_N], cis[MAX_N]; /* subroutines */ int ancestor(int u, int d) { for (int i = BN - 1; i >= 0; i--) if ((d >> i) & 1) u = ps[u][i]; return u; } int find_lca(int u, int v) { if (ds[u] > ds[v]) swap(u, v); v = ancestor(v, ds[v] - ds[u]); if (u == v) return u; for(int i = BN - 1; i >= 0; i--) if (ps[u][i] != ps[v][i]) u = ps[u][i], v = ps[v][i]; return ps[u][0]; } /* main */ int main() { int n, qn; scanf("%d%d", &n, &qn); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); u--, v--; nbrs[u].push_back(v); nbrs[v].push_back(u); } ps[0][0] = -1, ds[0] = 0; for (int u = 0; u >= 0;) { vi &nbru = nbrs[u]; int up = ps[u][0]; if (cis[u] < nbru.size()) { int v = nbru[cis[u]++]; if (v != up) { ps[v][0] = u, ds[v] = ds[u] + 1; u = v; } } else { ss[u]++; if (up >= 0) ss[up] += ss[u]; u = up; } } for (int i = 0; i < BN - 1; i++) for (int u = 0; u < n; u++) ps[u][i + 1] = (ps[u][i] >= 0) ? ps[ps[u][i]][i] : -1; while (qn--) { int s, t; scanf("%d%d", &s, &t); s--, t--; if ((ds[s] - ds[t]) & 1) { puts("0"); continue; } if (ds[s] > ds[t]) swap(s, t); int w = find_lca(s, t); int d = (ds[s] + ds[t] - ds[w] * 2) / 2; if (ds[s] != ds[t]) { int a0 = ancestor(t, d), a1 = ancestor(t, d - 1); printf("%d\n", ss[a0] - ss[a1]); } else { int a0 = ancestor(s, d - 1), a1 = ancestor(t, d - 1); printf("%d\n", n - (ss[a0] + ss[a1])); } } return 0; }