#line 1 "c.cpp" #include #include #define rep(i,n) for (int i = 0; i < int(n); ++i) #define repp(i,m,n) for (int i = m; i < int(n); ++i) #define reb(i,n) for (int i = int(n)-1; i >= 0; --i) #define all(v) v.begin(),v.end() using namespace std; using namespace atcoder; using ll = long long; using ull = unsigned long long; using ld = long double; using P = pair; using PL = pair; using pdd = pair; using pil = pair; using pli = pair; templateistream &operator>>(istream &is,vector &v){for(auto &e:v)is>>e;return is;} templatebool range(T a,T b,T x){return (a<=x&&xbool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));} template T rev(const T& str_or_vec){T res = str_or_vec; reverse(res.begin(),res.end()); return res; } templatebool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;} templatebool chmax(T &a,const T &b){if(avoid uniq(vector &v){sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());} templatevoid print(pair a); templatevoid print(vector v); templatevoid print(vector> v); void print(){ putchar(' '); } void print(bool a){ printf("%d", a); } void print(int a){ printf("%d", a); } void print(long a){ printf("%ld", a); } void print(long long a){ printf("%lld", a); } void print(char a){ printf("%c", a); } void print(char a[]){ printf("%s", a); } void print(const char a[]){ printf("%s", a); } void print(long double a){ printf("%.15Lf", a); } void print(const string& a){ for(auto&& i : a) print(i); } void print(unsigned int a){ printf("%u", a); } void print(unsigned long long a) { printf("%llu", a); } template void print(const T& a){ cout << a; } int out(){ putchar('\n'); return 0; } template int out(const T& t){ print(t); putchar('\n'); return 0; } template int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; } templatevoid print(pair a){print(a.first);print(),print(a.second);} templatevoid print(vector v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}} templatevoid print(vector> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}} void yes(){out("Yes");} void no (){out("No");} void yn (bool t){if(t)yes();else no();} void fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout< dx = {0,1,0,-1,1,1,-1,-1}; const vector dy = {1,0,-1,0,1,-1,-1,1}; const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string alp = "abcdefghijklmnopqrstuvwxyz"; const string NUM = "0123456789"; } // namespace noya2 using namespace noya2; using mint = modint998244353; //using mint = modint1000000007; //using mint = modint; void out(mint a){out(a.val());} void out(vector a){vector b(a.size()); rep(i,a.size()) b[i] = a[i].val(); out(b);} void out(vector> a){for (auto v : a) out(v);} #line 2 "rerooting_new.hpp" #line 4 "rerooting_new.hpp" namespace noya2 { using namespace std; template struct Rerooting { struct edge{ int to, idx, xdi; }; Rerooting (int _n = 0) : n(_n) { es.resize(n);} void add_edge(int u, int v, int idx1, int idx2){ es[u].push_back({v,idx1,idx2}); es[v].push_back({u,idx2,idx1}); } vector build(int v = 0){ root = v; outs.resize(n); subdp.resize(n); in.resize(n), up.resize(n); int tnow = 0; dfs(root,-1,tnow); return subdp; } vector reroot(){ reverse_edge.resize(n); reverse_edge[root] = e(); reverse_dp.resize(n); answers.resize(n); bfs(root); return answers; } V get(int r, int v){ if (r == v) return answers[r]; if (!(in[v] < in[r] && up[r] <= up[v])) return subdp[v]; int le = 0, ri = outs[v].size(); while (ri - le > 1){ int md = (le + ri) / 2; if (in[es[v][md].to] <= in[r]) le = md; else ri = md; } return reverse_dp[es[v][le].to]; } const vector& operator[](int idx) const { return es[idx]; } private: int n, root; vector> es; vector> outs; vector reverse_edge; vector subdp, reverse_dp, answers; vector in, up; void dfs(int v, int p, int &t){ E val = e(); in[v] = t++; for (auto &u : es[v]){ if (u.to == p && u.to != es[v].back().to) swap(u,es[v].back()); if (u.to == p) continue; dfs(u.to,v,t); E nval = put_edge(subdp[u.to],u.idx); outs[v].emplace_back(nval); val = merge(val,nval); } subdp[v] = put_vertex(val,v); up[v] = t; } void bfs(int v){ int siz = outs[v].size(); vector lui(siz+1), rui(siz+1); lui[0] = e(), rui[siz] = e(); for (int i = 0; i < siz; i++) lui[i+1] = merge(lui[i],outs[v][i]); for (int i = siz-1; i >= 0; i--) rui[i] = merge(outs[v][i],rui[i+1]); for (int i = 0; i < siz; i++){ reverse_dp[es[v][i].to] = put_vertex(merge(merge(lui[i],rui[i+1]),reverse_edge[v]),v); reverse_edge[es[v][i].to] = put_edge(reverse_dp[es[v][i].to],es[v][i].xdi); bfs(es[v][i].to); } answers[v] = put_vertex(merge(lui[siz],reverse_edge[v]), v); } }; } // namespace noya2 #line 2 "Tree-core.hpp" #line 5 "Tree-core.hpp" namespace noya2 { using namespace std; struct naiveTree { // undirected unweighted tree naiveTree (int _n = 0) : n(_n){ init();} void add_edge(int u, int v, int id = -1){ es0[u].emplace_back(v); es0[v].emplace_back(u); es1[u].emplace_back(v,id); es1[v].emplace_back(u,id); } void remake(int new_n){ es0.clear(); es1.clear(); vis.clear(); n = new_n; init(); } bool yet(int v){ return vis[v] == 0;} void visit(int v) { vis[v]++;} void reset(int v = -1){ if (v == -1) fill(vis.begin(),vis.end(),0); else vis[v] = 0; } const vector& operator[](int idx) const { return es0[idx];} const vector>& operator()(int idx) const {return es1[idx];} private: int n; vector> es0; vector>> es1; vector vis; void init(){ es0.resize(n); es1.resize(n); vis.resize(n,0); } }; struct usefulTree { // rooted tree usefulTree (int _n = 0, int _root = 0) : n(_n), root(_root) { init();} void add_edge(int u, int v){ es[u].emplace_back(v); es[v].emplace_back(u); } void remake(int new_n, int new_root = 0){ es.clear(); vis.clear(); n = new_n, root = new_root; init(); } bool yet(int v){ return vis[v] == 0;} void visit(int v) { vis[v]++;} void reset(int v = -1){ if (v == -1) fill(vis.begin(),vis.end(),0); else vis[v] = 0; } const vector& operator[](int idx) const { return es[idx];} int parent(int v){ return par[0][v];} int subtree_size(int v){ if (sub[v] != -1) return sub[v]; sub[v] = 1; for (int child : es[v]){ if (child != par[0][v]) sub[v] += subtree_size(child); } return sub[v]; } int depth(int v){ return dep[v];} int lca(int u, int v){ if (dep[u] > dep[v]) swap(u,v); for (int i = 0; i < p2size; i++) if ((dep[v] - dep[u]) >> i & 1) v = par[i][v]; if (u == v) return u; for (int i = p2size-1; i >= 0; i--){ if (par[i][u] != par[i][v]){ u = par[i][u]; v = par[i][v]; } } return par[0][u]; } int jump_to_root(int from, int d){ for (int i = 0; i < p2size; i++){ if ((d >> i & 1) == 1 && from != -1) from = par[i][from]; } return from; } int jump(int from, int to, int d){ int l = lca(from,to); if (d <= dep[from] - dep[l]){ return jump_to_root(from,d); } d -= dep[from] - dep[l]; if (d <= dep[to] - dep[l]){ return jump_to_root(to,dep[to]-dep[l]-d); } return -1; } vector path(int from, int to){ int l = lca(from,to); int nf = from, nt = to; vector pf = {from}, pt = {to}; while (nf != l){ nf = par[0][nf]; pf.emplace_back(nf); } while (nt != l){ nt = par[0][nt]; pt.emplace_back(nt); } for (int i = pt.size()-2; i >= 0; i--) pf.emplace_back(pt[i]); return pf; } int dist(int u, int v){ return dep[u] + dep[v] - 2 * dep[lca(u,v)];} void build(){ par.clear(); dep.clear(); sub.clear(); p2size = 1; int _ni = 1; // _ni = 2^(p2size - 1), n-1 <= 2^(p2size - 1) must be holded while (_ni < n-1) p2size++, _ni <<= 1; par.resize(p2size,vector(n,-1)); dep.resize(n,-1); sub.resize(n,-1); queue que; que.push(root); dep[root] = 0; while (!que.empty()){ int p = que.front(); que.pop(); for (int to : es[p]){ if (dep[to] == -1){ par[0][to] = p; dep[to] = dep[p] + 1; que.push(to); } } } for (int i = 1; i < p2size; i++){ for (int v = 0; v < n; v++){ if (par[i-1][v] == -1) continue; par[i][v] = par[i-1][par[i-1][v]]; } } } private: int n, root; vector> es; vector vis; int p2size; vector> par; vector dep, sub; void init(){ es.resize(n); vis.resize(n,0); } }; /* point hld (commutative) vector a(n); // vertex v has a[v] hldTree g(n); segtree seg(n); rep(i,n) seg.set(i,a[g.ord(i)]); // pre <-> ord (pre(v) = i, ord(i) = v) update query : int v; S x; cin >> v >> x; seg.set(g.pre(v),x); product query : S ans = e(); auto f = [&](int l, int r){ ans = op(ans,seg.prod(l,r)); }; int u, v; cin >> u >> v; ans = e(); g.path_query(u, v, true, f); cout << ans << endl; */ /* edge hld (commutative) vector b(n-1); // edge i has b[i] vector a(n); // vertex v has a[v] rep(v,n) a[v] = (v == root ? e() : b[g.edge(v)]); update query : int id; S x; cin >> id >> x; // edge id seg.set(g.pre(who(id)),x); // edge <-> who (edge(v) = i, who(i) = v) */ struct hldTree { hldTree (int _n = 0, int _root = 0) : n(_n), root(_root) { init();} void add_edge(int u, int v, int id){ // id must be 0 <= id < n es[u].emplace_back(v,id); es[v].emplace_back(u,id); } void remake(int new_n, int new_root = 0){ es.clear(); size.clear(); par.clear(); dep.clear(); up.clear(); down.clear(); nxt.clear(); order.clear(); edges.clear(); whose.clear(); n = new_n, root = new_root; init(); } void build(){ dfs_init(root); int t = 0; dfs_hld(root,t); } int lca(int u, int v){ while (nxt[u] != nxt[v]){ if (down[u] < down[v]) swap(u,v); u = par[nxt[u]]; } return dep[u] < dep[v] ? u : v; } int dist(int u, int v){ return dep[u] + dep[v] - 2 * dep[lca(u,v)]; } int parent(int v){ return par[v];} int depth(int v){ return dep[v];} int subtree_size(int v){ return size[v];} int pre(int v){ return down[v];} int post(int v){ return up[v];} int ord(int i){ return order[i];} int who(int i){ return whose[i];} int edge(int v){ return edges[v];} template void path_query(int u, int v, bool vertex, const F &f){ // f is function takes (left, right) as argument, range = [left,right). int l = lca(u,v); for (auto &p : ascend(u,l)){ int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1) s > t ? f(t,s) : f(s,t); } if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point for (auto &p : descend(l,v)){ int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1) s > t ? f(t,s) : f(s,t); } } template void path_noncommutative_query(int u, int v, bool vertex, const F &f){ // op(l,r) != op(r,l), so prod[u->...->v] != prod[v->...->u] int l = lca(u,v); for (auto &p : ascend(u,l)){ int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1) f(s,t); // le > ri ok } if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point for (auto &p : descend(l,v)){ int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1) f(s,t); // le > ri ok } } template void subtree_query(int v, bool vertex, const F &f){ f(down[v] + (vertex ? 0 : 1), up[v]); } const vector>& operator()(int idx) const { return es[idx];} private: int n, root; vector>> es; vector size, par, dep, up, down, nxt; // nxt[i] : most shallow vertex in connected component of vertex i vector order, edges, whose; // order[i] is ith vertex visited on Euler tour, vertex v has edges[v] (root has no edge), edges^-1 = whose void init(){ es.resize(n); size.resize(n,0); par.resize(n,root); dep.resize(n,0); up.resize(n,-1); down.resize(n,-1); nxt.resize(n,root); order.resize(n,-1); edges.resize(n,-1); whose.resize(n,-1); } void dfs_init(int cur){ size[cur] = 1; for (auto &e : es[cur]){ if (e.first == par[cur]){ if (es[cur].size() >= 2 && e.first == es[cur][0].first){ swap(es[cur][0],es[cur][1]); // if cur is not leaf, vs[cur][0] is not cur's parent } else continue; } par[e.first] = cur; edges[e.first] = e.second; whose[e.second] = e.first; dep[e.first] = dep[cur] + 1; dfs_init(e.first); size[cur] += size[e.first]; if (size[e.first] > size[es[cur][0].first]){ swap(e,es[cur][0]); // to maximize vs[cur][0]'s subtree_size } } } void dfs_hld(int cur, int &tnow){ down[cur] = tnow++; // down[0,...,n-1] is permutation of 0,...,n-1 order[down[cur]] = cur; for (auto e : es[cur]){ if (e.first == par[cur]) continue; nxt[e.first] = (e.first == es[cur][0].first ? nxt[cur] : e.first); dfs_hld(e.first,tnow); } up[cur] = tnow; // up[0,...,n-1] is NOT permutation, up[*] <= n } vector> ascend(int u, int v) const { // [u,v), depth[u] > depth[v] vector> res; while (nxt[u] != nxt[v]){ res.emplace_back(down[u],down[nxt[u]]); // [s1,t1], [s2,t2], ... u = par[nxt[u]]; } if (u != v) res.emplace_back(down[u],down[v]+1); // [s,t). v is not in the range (down[] is ordered opposite direction of depth) return res; } vector> descend(int u, int v) const { // (u,v], depth[u] < depth[v] if (u == v) return {}; if (nxt[u] == nxt[v]){ return {pair(down[u]+1,down[v])}; // (s,t]. u is not in the range } vector> res = descend(u,par[nxt[v]]); res.emplace_back(down[nxt[v]],down[v]); // [s1,t1], [s2,t2], ... return res; } }; } // namespace noya2 #line 78 "c.cpp" int op(int a, int b){ return a + b; } int e(){ return 0; } int pute(int e, int i){ return e; } int putv(int v, int i){ return v + 1; } void solve(){ int n, q; cin >> n >> q; usefulTree g(n); Rerooting rg(n); rep(i,n-1){ int u, v; cin >> u >> v; u--, v--; g.add_edge(u,v); rg.add_edge(u,v,i,i); } g.build(); rg.build(); rg.reroot(); while (q--){ int u, v; cin >> u >> v; u--, v--; int d = g.dist(u,v); if (d % 2 == 1){ out(0); continue; } int c = g.jump(u,v,d/2); out(rg.get(u,c) + rg.get(v,c) - n); } } int main(){ fast_io(); int t = 1; //cin >> t; while(t--) solve(); }