#include #include #define all(a) begin(a),end(a) #define fin(...) rr vo(pp(__VA_ARGS__)) #define ep(...) emplace(__VA_ARGS__) #define eb(...) emplace_back(__VA_ARGS__) #define ef(...) else if(__VA_ARGS__) #define wh(...) while(__VA_ARGS__) #define lb(...) lower_bound(__VA_ARGS__) #define ub(...) upper_bound(__VA_ARGS__) #define ov4(a,b,c,d,e,...) e #define ov5(a,b,c,d,e,f,...) f #define fo1(b) for(ll ii=0,bb=(ll)(b);ii=0;--i) #define of3(i,a,b) for(ll i=(ll)(a)-1,bb=(ll)(b);i>=bb;--i) #define of4(i,a,b,c) for(ll i=(ll)(a)-1,bb=(ll)(b),cc=(ll)(c);i>=bb;i-=cc) #define of(...) ov4(__VA_ARGS__,of4,of3,of2)(__VA_ARGS__) #define fe2(a,v) for(au&&a:v) #define fe3(a,b,v) for(au&&[a,b]:v) #define fe4(a,b,c,v) for(au&&[a,b,c]:v) #define fe(...) ov4(__VA_ARGS__,fe4,fe3,fe2)(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;li(__VA_ARGS__) #define DD(...) dd __VA_ARGS__;li(__VA_ARGS__) #define ST(...) str __VA_ARGS__;li(__VA_ARGS__) #define SS(n,a) strs a(n);li(a) #define U2(h,w,a) u2 a(h,w);li(a) #define UL2(n,a) u1 a(n);li(a) #define UL3(n,a,b) u1 a(n),b(n);li(a,b) #define UL4(n,a,b,c) u1 a(n),b(n),c(n);li(a,b,c) #define UL5(n,a,b,c,d) u1 a(n),b(n),c(n),d(n);li(a,b,c,d) #define UL(...) ov5(__VA_ARGS__,UL5,UL4,UL3,UL2)(__VA_ARGS__) #define UV3(n,a,b) u1 a(n),b(n);vi(a,b) #define UV4(n,a,b,c) u1 a(n),b(n),c(n);vi(a,b,c) #define UV5(n,a,b,c,d) u1 a(n),b(n),c(n),d(n);vi(a,b,c,d) #define UV(...) ov5(__VA_ARGS__,UV5,UV4,UV3)(__VA_ARGS__) #define I template #define J typename #define O operator #define rr return #define uu using #define au auto #define cs const namespace atcoder{}namespace my{void main();void solve();}int main(){my::main();} namespace my{uu namespace std;uu namespace atcoder; uu is=istream;uu os=ostream;uu vo=void;uu bo=bool;uu dd=long double;uu u8=uint8_t; uu ll=long long;uu i64=ll;uu i128=__int128_t;uu ul=unsigned long long;uu u64=ul;uu u128=__uint128_t; uu ml=modint998244353;is&O>>(is&i,ml&x){ll t;i>>t;x=t;rr i;}os&O<<(os&o,cs ml&x){rr o<uu v1=vector; Istruct v2:v1>{uu v1>::v1;v2(ll a,ll b,T x={}){this->resize(a,v1(b,x));}}; Istruct v3:v1>{uu v1>::v1;v3(ll a,ll b,ll c,T x={}){this->resize(a,v2(b,c,x));}}; Istruct v4:v1>{uu v1>::v1;v4(ll a,ll b,ll c,ll d,T x={}){this->resize(a,v3(b,c,d,x));}}; Istruct v5:v1>{uu v1>::v1;v5(ll a,ll b,ll c,ll d,ll e,T x={}){this->resize(a,v4(b,c,d,e,x));}}; uu u1=v1;uu u2=v2;uu u3=v3;uu u4=v4;uu m1=v1;uu m2=v2;uu m3=v3;uu m4=v4;uu m5=v5; uu str=string;uu strs=v1;str sp=" ",nc="",nl="\n";str Yes(bo y=1){rr y?"Yes":"No";}str No(){rr Yes(0);} Istruct cp{ A a;B b;cp():a(),b(){}cp(A a,B b):a(a),b(b){}cp(pairp):a(p.first),b(p.second){}cp O-()cs{rr cp(-a,-b);} cp&O+=(cs cp&c){a+=c.a,b+=c.b;rr*this;}cp O+(cs cp&c)cs{rr cp{*this}+=c;}bo O==(cs cp&c)cs{rr a==c.a&&b==c.b;} cp&O-=(cs cp&c){a-=c.a,b-=c.b;rr*this;}cp O-(cs cp&c)cs{rr cp{*this}-=c;}bo O!=(cs cp&c)cs{rr a!=c.a||b!=c.b;} bo O<(cs cp&c)cs{rr a!=c.a?a=(cs cp&c)cs{rr!(*this>(is&i,cp&c){rr i>>c.a>>c.b;} bo O>(cs cp&c)cs{rr a!=c.a?a>c.a:b>c.b;}bo O<=(cs cp&c)cs{rr!(*this>c);}friend os&O<<(os&o,cs cp&c){rr o<;uu cls=v1; Istruct tr{ A a;B b;C c;tr():a(),b(),c(){}tr(A a,B b,C c):a(a),b(b),c(c){}bo O==(cs tr&t)cs{rr a==t.a&&b==t.b&&c==t.c;} bo O<(cs tr&t)cs{rr a!=t.a?a>(is&i,tr&t){rr i>>t.a>>t.b>>t.c;} bo O>(cs tr&t)cs{rr a!=t.a?a>t.a:b!=t.b?b>t.b:c>t.c;}friend os&O<<(os&o,cs tr&t){rr o<;uu tls=v1; Istruct set:std::set{set(){}set(cs v1&a){fe(e,a)this->ep(e);}}; Istruct uset:unordered_set{uset(){}uset(cs v1&a){fe(e,a)this->ep(e);}}; Istruct mset:multiset{mset(){}mset(cs v1&a){fe(e,a)this->ep(e);} vo erase(cs T&x){if(this->count(x))multiset::erase(this->find(x));}}; uu mp=map;Iuu umap=unordered_map;uu ump=umap;uu sl=set;uu usl=uset;uu msl=mset; Iuu qmax=priority_queue;Iuu qmin=priority_queue,greater>;uu dl=deque;Iuu fn=function; ll oo=3e18;dd ee=1e-12,pi=acosl(-1);u1 dx{-1,0,1,0,-1,1,1,-1},dy{0,-1,0,1,-1,-1,1,1}; ul rnd(){static ul x=98,y=67,z=12,w=81;ul t=(x^(x<<11));x=y,y=z,z=w;rr(w=(w^(w>>19))^(t^(t>>8)));} ll rnd_rng(ll r){rr rnd()%r;}ll rnd_rng(ll l,ll r){rr rnd()%(r-l)+l;} bo odd(ll x){rr x&1;}bo eve(ll x){rr~x&1;}ll pm1(ll x){rr 1-2*(x&1);}ll pw2(ll n){rr 1LL<T sq(cs T&a){rr a*a;} ll cbr(ll x){if(x<=1)rr x;ll r=cbrtl(x)-1;wh(r+1<=x/(r+1)/(r+1))++r;rr r;}IT cb(cs T&a){rr a*a*a;} ll msb(ll x){rr x?63-__builtin_clzll(x):-1;}ll lsb(ll x){rr x?__builtin_ctzll(x):-1;} ll l2(u128 x){x|=1;ll r=0;wh(x)x>>=1,++r;rr r;}ll l10(u128 x){x|=1;ll r=0;wh(x)x/=10,++r;rr r;} u1 io(ll n,ll x=0){u1 a(n);fo(i,n)a[i]=i+x;rr a;}bo in(ll l,ll m,ll r){rr l<=m&&mll len(cs T&a){rr a.size();}Ibo mu(cs T&a){rr !len(a);} Ibo at(T S,ll i){rr S>>i&1;}IT at(cs v1&v,ll i){ll n=len(v);rr v[(i%n+n)%n];} IT flo(T x,T y){rr(y<0?flo(-x,-y):(x>=0?x/y:(x+1)/y-1));}Ill sgn(cs T&a){rr(a>ee)-(a<-ee);} IT cei(T x,T y){rr(y<0?cei(-x,-y):(x<=0?x/y:(x-1)/y+1));}Ill sgn(cs T&a,cs U&b){rr sgn(a-b);} IT rect(cs v1&s,ll l,ll r){assert(0<=l&&l<=r&&r<=len(s));T t{};if(r)t+=s[r-1];if(l)t-=s[l-1];rr t;} Iau&srt(v1&a){sort(all(a));rr a;}Iau&rv(v1&a){reverse(all(a));rr a;} Iau&rsr(v1&a){srt(a),rv(a);rr a;}Iau&uq(v1&a){srt(a),a.erase(unique(all(a)),end(a));rr a;} Iau slice(cs v1&a,ll l,ll r){rr v1(begin(a)+l,begin(a)+r);} Idecltype(au)bg(T&a){rr*begin(a);}Ivo af(v1&v,U e=U{}){v.ep(begin(v),e);} Idecltype(au)rb(T&a){rr*rbegin(a);}Ivo df(v1&v){v.erase(begin(v));} IT pop(v1&v){T r=rb(v);v.pop_back();rr r;} IT pop(qmax&q){T r=q.top();q.pop();rr r;}IT pof(deque&q){T r=bg(q);q.pop_front();rr r;} IT pop(qmin&q){T r=q.top();q.pop();rr r;}IT pob(deque&q){T r=rb(q);q.pop_back();rr r;} Iau&O++(v1&v,int){fe(e,v)++e;rr v;}Iau&O--(v1&v,int){fe(e,v)--e;rr v;} Iau&O^=(v1&v,cs v1u){copy(all(u),back_inserter(v));rr v;}Iau O^(v1v,cs v1&u){rr v^=u;} Ibo amax(T&a,cs U&b){rr aau max(T...a){rr max(initializer_list>{a...});} Ibo amin(T&a,cs U&b){rr a>b?a=b,1:0;}Iau min(T...a){rr min(initializer_list>{a...});} IT max(cs v1&a){rr*max_element(all(a));}Iau max(cs v2&a){T r=bg(bg(a));fe(v,a)amax(r,max(v));rr r;} IT min(cs v1&a){rr*min_element(all(a));}Iau min(cs v2&a){T r=bg(bg(a));fe(v,a)amin(r,min(v));rr r;} Ill lbs(cs v1&a,cs U&b){rr lb(all(a),b)-begin(a);}IT sum(cs v1&a){rr accumulate(all(a),T{});} Ill ubs(cs v1&a,cs U&b){rr ub(all(a),b)-begin(a);}IT sum(cs v2&a){T r{};fe(e,a)r+=sum(e);rr r;} struct fio{fio(){cin.tie(0);ios::sync_with_stdio(0);cout<os&O<<(os&o,cs pair&p){rr o<os&O<<(os&o,qmaxq){wh(len(q))o<os&O<<(os&o,cs set&v){fe(e,v)o<os&O<<(os&o,qminq){wh(len(q))o<os&O<<(os&o,cs mset&v){fe(e,v)o<os&O<<(os&o,cs v1&v){fe(e,v)o<os&O<<(os&o,cs map&m){fe(p,m)o<os&O<<(os&o,cs v2&v){fe(e,v)o<is&O>>(is&i,v1&v){fe(e,v)i>>e;rr i;}Ill li(T&...a){(cin>>...>>a);rr 0;} Ivo vi(ll i,T&...a){(cin>>...>>a[i]);}Ivo vi(v1&a,A&...b){fo(i,len(a))vi(i,a,b...);} ll pp(){cout<ll pp(cs T&a,cs A&...b){cout<; u2 tou(cs graph&g){ll n=len(g);u2 a(n);fo(u,n)fe(v,w,g[u])a[u].eb(v);rr a;} graph tog(cs u2&a){ll n=len(a);graph g(n);fo(u,n)fe(v,a[u])g[u].eb(v);rr g;} au mgi(u2&g,ll m){UV(m,a,b);a--,b--;fo(i,m)g[a[i]].eb(b[i]),g[b[i]].eb(a[i]);rr tuple(a,b);} au ygi(u2&g,ll m){UV(m,a,b);a--,b--;fo(i,m)g[a[i]].eb(b[i]);rr tuple(a,b);}au ti(u2&g){rr mgi(g,len(g)-1);} u1 en(cs str&s,cs str&b="a"){ll n=len(s);u1 r(n,-1);fo(i,n)if(len(b)==1)r[i]=s[i]-b[0];else fo(j,len(b))if(s[i]==b[j])r[i]=j;rr r;} au de(cs u1&a,cs str&b){ll n=len(a);str r(n,'$');fo(i,n)if(len(b)==1)r[i]=a[i]+b[0];else fo(j,len(b))if(a[i]==j)r[i]=b[j];rr r;} u2 en(cs strs&s,cs str&b="a"){u2 r;fe(e,s)r.eb(en(e,b));rr r;} au de(cs u2&a,cs str&b){ll n=len(a);strs r(n);fo(i,n)r[i]=de(a[i],b);rr r;} str ins(){str s;li(s);rr s;}strs inss(ll n){strs s(n);li(s);rr s;}u1 abc(){rr en(ins());}u2 abcs(ll n){rr en(inss(n));} Istruct rec:F{rec(F&&f):F(forward(f)){}Idecltype(au)O()(T&&...a)cs{rr F::O()(*this,std::forward(a)...);}}; Iu1 zp(A&...a){u1 v;fe(e,{a...})v^=e;uq(v);(vo)initializer_list{(vo([&](u1&a){fe(e,a)e=lbs(v,e);}(a)),u1())...};rr v;} Iu1 vs(cs F&f,A&...a){ll n;fe(e,{a...})n=len(e);u1 o=io(n);sort(all(o),f); (vo)initializer_list{(vo([&](u1&a){u1 c=a;fo(i,n)a[i]=c[o[i]];}(a)),u1())...};rr o;} ll bsl(cs fn&j,ll o,ll n){wh(abs(o-n)>1)(j((o+n)/2)?o:n)=(o+n)/2;rr o;} dd bsd(cs fn&j,dd o,dd n){fo(l2(abs(o-n)/ee))(j((o+n)/2)?o:n)=(o+n)/2;rr o;} Iau zt(v1a){fo(i,1,len(a))a[i]+=a[i-1];rr a;} au rle(cs u1&a){cls r;fe(e,a)len(r)&&e==rb(r).a?++rb(r).b,0:(r.eb(e,1),0);rr r;} au rce(cs u1&a){cls r;ump m;fe(e,a)++m[e];fe(k,v,m)r.eb(k,v);rr srt(r);} u1 divs(ll n){u1 r;for(ll i=1;i*i<=n;i++)if(n%i==0)r.eb(i),r.eb(n/i);rr uq(r);} mp fact(ll n){ump m;for(ll i=2;i*i<=n;i++)wh(n%i==0)++m[i],n/=i;if(n>1)++m[n];rr mp(all(m));} i128 inv(i128 a,i128 m){a=(a%m+m)%m;i128 b=m,u=1,v=0;wh(b)u-=a/b*v,swap(u,v),a-=a/b*b,swap(a,b);rr(u%m+m)%m;} i128 pow(i128 x,i128 n,i128 m=0){if(n<0)n=-n,x=inv(x,m);i128 r=1;wh(n){if(n&1)r*=x;x*=x;if(m)r%=m,x%=m;n>>=1;}rr r;} struct lca_doubling{ graph g; ll n,L,r; u2 pa; u1 dep,dst; lca_doubling(cs u2&g,ll r=0):lca_doubling(tog(g),r){} lca_doubling(cs graph&g,ll r=0):g(g),n(len(g)),L(l2(n)),r(r),pa(l2(n),n),dep(n),dst(n){ build(); } vo build(){ pa[0][r]=-1; dep[r]=dst[r]=0; dequeq; q.eb(r,-1); wh(len(q)){ au[u,p]=pof(q); fe(v,w,g[u])if(v!=p){ pa[0][v]=u; dep[v]=dep[u]+1; dst[v]=dst[u]+w; q.eb(v,u); } } fo(k,L-1){ fo(i,n){ if(pa[k][i]==-1)pa[k+1][i]=-1; else pa[k+1][i]=pa[k][pa[k][i]]; } } } ll par(ll u,ll k=1){//uのk個上の祖先 if(dep[u]dfs_order(cs u2&g,ll s=0){ ll n=len(g); u1 ord,dep(n),par(n); tls st{{s,-1,0}}; wh(len(st)){ au[u,p,d]=pop(st); ord.eb(u),dep[u]=d,par[u]=p; fe(v,g[u])if(v!=p)st.eb(v,u,d+1); } rr{ord,dep,par}; } vo main(){ll T=1;fo(T)solve();} vo solve(){ LL(N,Q); u2 g(N);ti(g); lca_doubling lca(g); au[ord,dep,par]=dfs_order(g); rv(ord); u1 sz(N); fe(u,ord){ sz[u]=1; fe(v,g[u])if(v!=par[u])sz[u]+=sz[v]; } fo(Q){ LL(u,v);u--,v--; if(u==v){ pp(N); continue; } ll d=lca.dist(u,v); if(d&1){ pp(0); continue; } if(dep[u]>dep[v])swap(u,v); ll m=lca.par(v,d/2); ll res; if(dep[u]==dep[v]){//u,vともにmの部分木に含まれる res=N; res-=sz[lca.par(u,d/2-1)]; res-=sz[lca.par(v,d/2-1)]; }else{ res=sz[m]; res-=sz[lca.par(v,d/2-1)]; } pp(res); } }}