#include typedef struct Edge { struct Edge *next; int v; } edge; void init_LCA(int N, edge* adj[], int par[][20], int depth[]) { static int i, u, w, x, q[200001], head, tail; static edge *p; for (u = 1, depth[0] = -1; u <= N; u++) { for (i = 0; i < 20; i++) par[u][i] = 0; depth[u] = -1; } par[1][0] = 0; depth[1] = 0; q[0] = 1; for (head = 0, tail = 1; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (depth[w] < 0) { par[w][0] = u; depth[w] = depth[u] + 1; for (i = 0, x = u; x != 0; x = par[x][i++]) par[w][i+1] = par[x][i]; q[tail++] = w; } } } } int get_LCA(int par[][20], int depth[], int u, int w) { int i; while (depth[u] > depth[w]) { for (i = 1; depth[par[u][i]] >= depth[w]; i++); u = par[u][i-1]; } while (depth[u] < depth[w]) { for (i = 1; depth[par[w][i]] >= depth[u]; i++); w = par[w][i-1]; } while (u != w) { for (i = 1; par[u][i] != par[w][i]; i++); u = par[u][i-1]; w = par[w][i-1]; } return u; } int main() { int i, N, Q, u, w; edge *adj[200001] = {}, e[400001], *p; scanf("%d %d", &N, &Q); for (i = 0; i < N - 1; i++) { scanf("%d %d", &u, &w); e[i*2].v = w; e[i*2+1].v = u; e[i*2].next = adj[u]; e[i*2+1].next = adj[w]; adj[u] = &(e[i*2]); adj[w] = &(e[i*2+1]); } static int par[200001] = {}, size[200001], q[200001], head, tail; par[1] = 1; q[0] = 1; for (head = 0, tail = 1; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (par[w] == 0) { par[w] = u; q[tail++] = w; } } } for (head--; head >= 0; head--) { u = q[head]; size[u]++; if (head > 0) size[par[u]] += size[u]; } int s, t, r; static int depth[200001], LCA_par[200001][20]; init_LCA(N, adj, LCA_par, depth); while (Q--) { scanf("%d %d", &s, &t); if ((depth[s] + depth[t]) % 2 != 0) { printf("0\n"); continue; } r = get_LCA(LCA_par, depth, s, t); if (depth[s] < depth[t]) { w = t; while (depth[w] > (depth[t] - depth[s]) / 2 + depth[r] + 1) { for (i = 1; depth[LCA_par[w][i]] > (depth[t] - depth[s]) / 2 + depth[r] + 1; i++); w = LCA_par[w][i-1]; } printf("%d\n", size[par[w]] - size[w]); } else if (depth[s] > depth[t]) { u = s; while (depth[u] > (depth[s] - depth[t]) / 2 + depth[r] + 1) { for (i = 1; depth[LCA_par[u][i]] > (depth[s] - depth[t]) / 2 + depth[r] + 1; i++); u = LCA_par[u][i-1]; } printf("%d\n", size[par[u]] - size[u]); } else { u = s; w = t; while (depth[u] > depth[r] + 1) { for (i = 1; depth[LCA_par[u][i]] > depth[r]; i++); u = LCA_par[u][i-1]; } while (depth[w] > depth[r] + 1) { for (i = 1; depth[LCA_par[w][i]] > depth[r]; i++); w = LCA_par[w][i-1]; } printf("%d\n", size[r] - size[u] - size[w]); } } fflush(stdout); return 0; }