#include #include // url #define rep(i,a,b) for(int i=a;i=a;i--) #define all(x) (x).begin(),(x).end() #define pb(x) push_back(x); templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b; const ll mod=998244353; // using mint = static_modint<1000000007>; //const ll mod=1e9+7; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; const string zton="0123456789"; const string atoz="abcdefghijklmnopqrstuvwxyz"; const string ATOZ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const ll inf=(1ll<<60); // const int inf=(1<<30); lld dist(lld x1,lld x2,lld y1,lld y2){ lld res=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); res=sqrt(abs(res)); return res; } lld arg(lld x,lld y){ const lld eps=1e-8; lld res=0; if(abs(x)+abs(y)<=eps)return 0.0; else if(abs(x)<=eps){ if(y>=0.0)return (M_PI/2); else return (M_PI/2+M_PI); } else if(abs(y)<=eps){ if(x>=0.0)return 0.0; else return M_PI; } res=atan2(abs(y),abs(x)); if(x<=0&&y>=0)res=(M_PI-res); else if(x<=0&&y<=0)res+=(M_PI); else if(x>=0&&y<=0)res=(M_PI*2-res); return res; } ll gcd(ll a,ll b){ if(a==0||b==0)return a+b; ll r; r=a%b; if(r==0){ return b; } else{ return gcd(b,r); } } typedef pair P; struct Edge { long long to; }; using Graph = vector>; /* LCA(G, root): 木 G に対する根を root として Lowest Common Ancestor を求める構造体 query(u,v): u と v の LCA を求める。計算量 O(logn) 前処理: O(nlogn)時間, O(nlogn)空間 */ struct LCA { vector> parent; // parent[k][u]:= u の 2^k 先の親 vector dist; // root からの距離 LCA(const Graph &G, int root = 0) { init(G, root); } // 初期化 void init(const Graph &G, int root = 0) { int V = G.size(); int K = 1; while ((1 << K) < V) K++; parent.assign(K, vector(V, -1)); dist.assign(V, -1); dfs(G, root, -1, 0); for (int k = 0; k + 1 < K; k++) { for (int v = 0; v < V; v++) { if (parent[k][v] < 0) { parent[k + 1][v] = -1; } else { parent[k + 1][v] = parent[k][parent[k][v]]; } } } } // 根からの距離と1つ先の頂点を求める void dfs(const Graph &G, int v, int p, int d) { parent[0][v] = p; dist[v] = d; for (auto e : G[v]) { if (e.to != p) dfs(G, e.to, v, d + 1); } } int query(int u, int v) { if (dist[u] < dist[v]) swap(u, v); // u の方が深いとする int K = parent.size(); // LCA までの距離を同じにする for (int k = 0; k < K; k++) { if ((dist[u] - dist[v]) >> k & 1) { u = parent[k][u]; } } // 二分探索で LCA を求める if (u == v) return u; for (int k = K - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } int get_post(int v,int u){ int K=parent.size(); int distu=dist[u]; int distv=dist[v]+1; for(int k=K-1;k>=0;k--){ if(dist[u]-distv>=(1<=0;k--){ if(dist[t]-target_dist>=(1<> n; // Graph g(n); // rep(i,0,n){ // int k;cin >> k; // rep(j,0,k){ // Edge c; // cin >> c.to; // g[i].push_back(c); // } // } // LCA aa=LCA(g,0); // int q;cin >> q; // rep(i,0,q){ // int u,v;cin >> u >> v; // cout << aa.query(u,v) << endl; // } int N; vector G[201010]; map F[201010]; int dfs(int cu,int pa){ F[cu][cu]=1; for(int to:G[cu]){ if(to==pa)continue; F[cu][to]=dfs(to,cu); F[cu][cu]+=F[cu][to]; } return F[cu][cu]; } int solve(LCA &lca,int s,int t){ if(s==t){ return 0; } int dists=lca.get_dist(s,0); int distt=lca.get_dist(t,0); if(dists==distt){ int u=lca.query(s,t); int s2=lca.get_post(u,s); int t2=lca.get_post(u,t); // if(s==14&&t==7){ // cout << s2 << " " << t2 << endl; // } // ans.pb(N-F[u][s2]-F[u][t2]); // if(N-F[u][s2]-F[u][t2]<0){ // cout << s << " " << t << endl; // } return N-F[u][s2]-F[u][t2]; } else{ int distst=lca.get_dist(s,t); if(distst%2==1){ // ans.pb(0); return 0; } else{ if(dists>distt){ swap(s,t); swap(dists,distt); } int u=lca.get_half(s,t); int t2=lca.get_post(u,t); // ans.pb(F[u][u]-F[u][t2]); return F[u][u]-F[u][t2]; } } } int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); int Q;cin >> N >> Q; Graph g(N); rep(i,0,N-1){ int a,b;cin >> a >> b; a--;b--; Edge c; c.to=b; g[a].push_back(c); c.to=a; g[b].push_back(c); G[a].pb(b); G[b].pb(a); } dfs(0,-1); LCA lca=LCA(g,0); vector ans; rep(i,0,Q){ int s,t;cin >> s >> t;s--;t--; ans.pb(solve(lca,s,t)); } // rep(s,0,N)rep(t,0,N){ // int cnt=0; // if(s==t){ // continue; // } // rep(u,0,N){ // if(u==s||t==u)continue; // if(lca.get_dist(s,u)==lca.get_dist(t,u))cnt++; // } // solve(lca,s,t); // cout << s << " " << t << " : " << solve(lca,s,t) << " " << cnt << endl; // } for(int x:ans){ cout << x << endl; } // rep(i,0,N){ // cout << F[i][i] << endl; // } }