#include using namespace atcoder; using mint = modint998244353; const long long MOD = 998244353; // using mint = modint1000000007; // const long long MOD = 1000000007; // using mint = modint;//mint::set_mod(MOD); #include #define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define repeq(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++) #define repreq(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--) #define endl '\n' // fflush(stdout); #define cYes cout << "Yes" << endl #define cNo cout << "No" << endl #define sortr(v) sort(v, greater<>()) #define pb push_back #define pob pop_back #define mp make_pair #define mt make_tuple #define FI first #define SE second #define ALL(v) (v).begin(), (v).end() #define INFLL 3000000000000000100LL #define INF 1000000100 #define PI acos(-1.0L) #define TAU (PI * 2.0L) using namespace std; typedef long long ll; typedef pair Pll; typedef tuple Tlll; typedef vector Vi; typedef vector VVi; typedef vector Vl; typedef vector VVl; typedef vector VVVl; typedef vector VTlll; typedef vector Vm; typedef vector VVm; typedef vector Vs; typedef vector Vd; typedef vector Vc; typedef vector Vb; typedef vector VPll; typedef priority_queue PQl; typedef priority_queue, greater> PQlr; /* inout */ ostream &operator<<(ostream &os, mint const &m) { os << m.val(); return os; } istream &operator>>(istream &is, mint &m) { long long n; is >> n, m = n; return is; } template ostream &operator<<(ostream &os, const vector &v) { int n = v.size(); rep(i, 0, n) { os << v[i] << " \n"[i == n - 1]; } return os; } template ostream &operator<<(ostream &os, const vector> &v) { int n = v.size(); rep(i, 0, n) os << v[i]; return os; } template ostream &operator<<(ostream &os, pair const &p) { os << p.first << ' ' << p.second; return os; } template ostream &operator<<(ostream &os, const map &mp) { for (auto &[key, val] : mp) { os << key << ':' << val << '\n'; } return os; } template ostream &operator<<(ostream &os, const set &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? ' ' : '\n'); itr++; } return os; } template ostream &operator<<(ostream &os, multiset &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? ' ' : '\n'); itr++; } return os; } template ostream &operator<<(ostream &os, queue q) { while (q.size()) { os << q.front(); q.pop(); os << " \n"[q.empty()]; } return os; } template ostream &operator<<(ostream &os, stack st) { vector v; while (st.size()) { v.push_back(st.top()); st.pop(); } reverse(ALL(v)); os << v; return os; } template ostream &operator<<(ostream &os, priority_queue pq) { vector v; while (pq.size()) { v.push_back(pq.top()); pq.pop(); } os << v; return os; } template istream &operator>>(istream &is, vector &v) { for (T &in : v) is >> in; return is; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } /* useful */ template int SMALLER(vector &a, T x) { return lower_bound(a.begin(), a.end(), x) - a.begin(); } template int orSMALLER(vector &a, T x) { return upper_bound(a.begin(), a.end(), x) - a.begin(); } template int BIGGER(vector &a, T x) { return a.size() - orSMALLER(a, x); } template int orBIGGER(vector &a, T x) { return a.size() - SMALLER(a, x); } template int COUNT(vector &a, T x) { return upper_bound(ALL(a), x) - lower_bound(ALL(a), x); } template bool chmax(T &a, S b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T &a, S b) { if (a > b) { a = b; return 1; } return 0; } template void press(T &v) { v.erase(unique(ALL(v)), v.end()); } template vector zip(vector b) { pair p[b.size() + 10]; int a = b.size(); vector l(a); for (int i = 0; i < a; i++) p[i] = mp(b[i], i); sort(p, p + a); int w = 0; for (int i = 0; i < a; i++) { if (i && p[i].first != p[i - 1].first) w++; l[p[i].second] = w; } return l; } template vector vis(vector &v) { vector S(v.size() + 1); rep(i, 1, S.size()) S[i] += v[i - 1] + S[i - 1]; return S; } ll dem(ll a, ll b) { return ((a + b - 1) / (b)); } ll dtoll(double d, int g) { return round(d * pow(10, g)); } string tobin(ll n, ll d) { string ret; rep(i, 0, d) { ret += (n % 2) ? '1' : '0'; n /= 2; } reverse(ALL(ret)); return ret; } const double EPS = 1e-10; void init() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(12); } // do {} while (next_permutation(ALL(vec))); /********************************** START **********************************/ void sol(); int main() { init(); int q = 1; // cin >> q; while (q--) sol(); return 0; } /********************************** SOLVE **********************************/ template struct Tree { int n, root; bool edgeCost; vector> gr, chi, par_exp; vector al, par, dep, subsize, euler; vector>> gr_edge_cost; vector> edge_info; /* HL分解用 */ vector> gr_hld; vector stsize, pathtop, in, out; Tree(int n = 0, bool edgeCost = false) : n(n), edgeCost(edgeCost) { gr = vector>(n); if (edgeCost) { gr_edge_cost.resize(n); } } Tree(vector> input) { n = input.size(); gr = vector>(n); int nw = 0; vector al(n); al[nw] = 1; queue q; q.push(nw); while (!q.empty()) { nw = q.front(); q.pop(); for (auto e : input[nw]) { if (!al[e]) { connect(nw, e); q.push(e); al[e] = 1; } } } } void connect(int s, int t) { gr[s].push_back(t); gr[t].push_back(s); } void connect(int s, int t, T c) { edge_info.push_back({s, t, gr[s].size(), gr[t].size(), c}); gr[s].push_back(t); gr[t].push_back(s); gr_edge_cost[s].push_back({t, c}); gr_edge_cost[t].push_back({s, c}); } void build_dfs(int d) { int sum_subsize = 0; for (auto &p : gr[d]) { if (al[p]) continue; al[p] = 1; par[p] = d; dep[p] = dep[d] + 1; chi[d].push_back(p); euler.push_back(d); build_dfs(p); sum_subsize += subsize[p]; } subsize[d] = sum_subsize + 1; euler.push_back(d); } void make(int _root) { root = _root; rep(i, 0, n) sort(ALL(gr[i])); al = par = subsize = dep = vector(n, 0); chi = vector>(n); euler = {}; al[root] = 1; par[root] = -1; dep[root] = 0; build_dfs(root); } pair rad() { int max_ind = -1, max_dep = -1; int s, t; make(0); rep(i, 0, n) if (chmax(max_dep, dep[i])) max_ind = i; make(max_ind); s = max_ind; max_ind = -1, max_dep = -1; rep(i, 0, n) if (chmax(max_dep, dep[i])) max_ind = i; t = max_ind; swap(s, t); // return max_dep; return make_pair(s, t); } void init_lca() { int ct = 0; while (n >= (1 << ct)) ct++; ct++; par_exp = vector>(ct, vector(n, -1)); rep(i, 0, n) par_exp[0][i] = par[i]; rep(i, 0, ct - 1) { rep(j, 0, n) { if (par_exp[i][j] == -1) par_exp[i + 1][j] = -1; else par_exp[i + 1][j] = par_exp[i][par_exp[i][j]]; } } } int lca(int u, int v) { int h = par_exp.size(); if (dep[u] > dep[v]) swap(u, v); for (int i = h - 1; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = par_exp[i][v]; } if (u == v) return u; for (int i = h - 1; i >= 0; i--) { if (par_exp[i][u] != par_exp[i][v]) { u = par_exp[i][u]; v = par_exp[i][v]; } } return par_exp[0][u]; } int up(int v, int k) { // 頂点 v から k 回親をたどる if (dep[v] < k) return -1; int h = par_exp.size(); for (int i = h - 1; i >= 0; i--) { if ((k >> i) & 1) v = par_exp[i][v]; } return v; } void BuildStsize(int u, int p) { stsize[u] = 1, par[u] = p; for (int &v : gr_hld[u]) { if (v == p) { if (v == gr_hld[u].back()) break; else swap(v, gr_hld[u].back()); } BuildStsize(v, u); stsize[u] += stsize[v]; if (stsize[v] > stsize[gr_hld[u][0]]) { swap(v, gr_hld[u][0]); } } } void BuildPath(int u, int p, int &tm) { in[u] = tm++; for (int v : gr_hld[u]) { if (v == p) continue; pathtop[v] = (v == gr_hld[u][0] ? pathtop[u] : v); BuildPath(v, u, tm); } out[u] = tm; } void init_hld() { gr_hld = gr; stsize = pathtop = in = out = vector(n); if (!par_exp.size()) init_lca(); int tm = 0; BuildStsize(root, -1); pathtop[root] = root; BuildPath(root, -1, tm); } vector> path_query(int a, int b, bool edgeQuery = false) { int pta = pathtop[a], ptb = pathtop[b]; vector> ret; while (pathtop[a] != pathtop[b]) { if (in[pta] > in[ptb]) { ret.push_back({in[pta], in[a] + 1}); a = par[pta], pta = pathtop[a]; } else { ret.push_back({in[ptb], in[b] + 1}); b = par[ptb], ptb = pathtop[b]; } } if (in[a] > in[b]) swap(a, b); ret.push_back({in[a], in[b] + 1}); if (edgeQuery) { int c = lca(a, b); for (auto &p : ret) { if (p.first == in[c]) p.first++; } } return ret; } vector> subtree_query(int d, bool edgeQuery = false) { return {{in[d] + edgeQuery, out[d]}}; } int hld_ind(int d) { return in[d]; } void bfs(int root) { // bfs template al = vector(n); al[root] = 1; queue q; stack st; q.push(root); st.push(root); vector order; while (!q.empty()) { int d = q.front(); order.pb(d); // 今見ている頂点 q.pop(); for (auto &p : gr[d]) { if (al[p]) continue; al[p] = 1; q.push(p); st.push(p); // ここで次の頂点を見ている } } while (!st.empty()) { int d = st.top(); st.pop(); order.pb(d); // ここでbfsの逆順に頂点をたどっている } } void dfs(int root) { val.resize(n); al = vector(n); al[root] = 1; dfs_(root); } Vl val; ll nw = 0; void dfs_(int d) { // dfs template if (edgeCost) { val[d] = nw; nw = 0; for (auto &[p, c] : gr_edge_cost[d]) { if (al[p]) continue; nw += c; al[p] = 1; dfs_(p); } return; } } }; /* 使い方 */ /* Tree tr(n) // 宣言 tr.connect(a,b) // 頂点 a と b をつなぐ tr.make(d) // 頂点 d を根としてpar,dep,chiを作り上げる tr.rad() // 木の直径を返す tr.init_lca() // lca,upを使う前に必ずこれをする tr.lca(a,b) // 頂点 a と b の共通の"先祖"で一番近い"頂点"を返す tr.up(v,k) // 頂点 v から k 回上に行った頂点を返す tr.init_hld() // HL分解の前準備 tr.hld_ind(d) // 頂点 d のHL分解上の index を返す tr.path_query(s,t) // sからtへのパス上を示す配列を返す tr.subtree_query(d) // sの部分木を示す配列を返す */ void sol() { int n, q; cin >> n >> q; Tree tr(n); rep(i, 0, n - 1) { int a, b; cin >> a >> b; a--, b--; tr.connect(a, b); } tr.make(0); tr.init_lca(); while (q--) { int qw, er; cin >> qw >> er; qw--, er--; int p = tr.lca(qw, er); int dis = abs(tr.dep[qw] - tr.dep[p]) + abs(tr.dep[er] - tr.dep[p]); if (dis % 2) { cout << 0 << endl; continue; } int ue = dis / 2; if (tr.dep[qw] < tr.dep[er]) swap(qw, er); int ui = tr.up(qw, ue); int ans = tr.subsize[ui]; if (tr.lca(ui, qw) == ui) { int ue2 = tr.up(qw, ue - 1); ans -= tr.subsize[ue2]; } swap(qw, er); if (tr.lca(ui, qw) == ui) { int ue2 = tr.up(qw, ue - 1); ans -= tr.subsize[ue2]; } cout << ans << endl; } }