//#define _GLIBCXX_DEBUG #include using namespace std; #define endl '\n' #define lfs cout<= (ll)(n); i--) using ll = long long; using ld = long double; const ll MOD1 = 1e9+7; const ll MOD9 = 998244353; const ll INF = 1e18; using P = pair; templatebool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} templatebool chmax(T1 &a,T2 b){if(avoid ans(bool x,T1 y,T2 z){if(x)cout<void debug(T &v,ll h,ll w,string sv=" "){for(ll i=0;ivoid debug(T &v,ll n,string sv=" "){if(n!=0)cout<vector>vec(ll x, ll y, T w){vector>v(x,vector(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vectordx={1,-1,0,0,1,1,-1,-1};vectordy={0,0,1,-1,1,-1,1,-1}; templatevector make_v(size_t a,T b){return vector(a,b);} templateauto make_v(size_t a,Ts... ts){return vector(a,make_v(ts...));} templateostream &operator<<(ostream &os, const pair&p){return os << p.first << " " << p.second;} templateostream &operator<<(ostream &os, const vector &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;} templatevoid rearrange(vector&ord, vector&v){ auto tmp = v; for(int i=0;ivoid rearrange(vector&ord,Head&& head, Tail&&... tail){ rearrange(ord, head); rearrange(ord, tail...); } //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); int popcount(ll x){return __builtin_popcountll(x);}; int poplow(ll x){return __builtin_ctzll(x);}; int pophigh(ll x){return 63 - __builtin_clzll(x);}; template< typename T = int > struct edge { int to; T cost; int id; edge() = default; edge(int to, T cost = 1, int id = 0):to(to), cost(cost), id(id){} operator int() const { return to; } }; template using Graph = vector>>; template Graph readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){ Graph ret(n); for(int es = 0; es < m; es++){ int u,v,w=1; cin>>u>>v;u-=indexed,v-=indexed; if(weighted)cin>>w; ret[u].emplace_back(v,w,es); if(!directed)ret[v].emplace_back(u,w,es); } return ret; } template Graph readParent(int n,int indexed=1,bool directed=true){ Graphret(n); for(int i=1;i>p; p-=indexed; ret[p].emplace_back(i); if(!directed)ret[i].emplace_back(p); } return ret; } template struct Lowlink{ Graph G; int size; vectorord,low,used; vectorarticulation; vector>bridge; Lowlink(Graph &g):G(g){ size = G.size(); ord.assign(size,-1); low.assign(size,-1); used.assign(size,0); int tmp=0; for(int i=0;i=2)is_articulation=true; if(is_articulation)articulation.push_back(k); return; } vectortecc_index;//二重辺連結成分の番号 Graphtecc_graph; int TECC(){ tecc_index.assign(G.size(),-1); int cnt=0; for(int i=0;i>()); for(int i=0;i>>bcc; stack>tmp; void BCC(){ used.assign(G.size(), false); for(int i=0;i= ord[k]){ bcc.emplace_back(); while(1){ auto e = tmp.top(); bcc.back().emplace_back(e); tmp.pop(); if(e.first == min(k, to) && e.second == max(k, to))break; } } } } } Graphbct_graph; vectorbct_index; vectorart; void makeBCT(){ assert(bcc.empty()); BCC(); art.assign(G.size(), false); bct_index.assign(G.size(), -1); for(auto v:articulation){ bct_index[v] = bct_graph.size(); bct_graph.emplace_back(); art[v] = true; } vectorcheck(G.size(),false); for(auto &ev:bcc){ int idx = bct_graph.size(); bct_graph.emplace_back(); queuerev; for(auto &e:ev){ for(auto &v:{e.first, e.second}){ if(art[v]&&!check[v]){ rev.push(v); check[v] = true; bct_graph[idx].emplace_back(bct_index[v]); bct_graph[bct_index[v]].emplace_back(idx); } else if(!art[v]){ bct_index[v] = idx; } } } while(!rev.empty()){ check[rev.front()] = false; rev.pop(); } } } }; template struct HLD{ int n; vectorsz;//部分木サイズ vectordep; vectorpar; vectorhead; Graph &g;//隣接リスト vector>edges;//データ構造に乗せるedge列 vectorin,out;//[in,out)で部分木、[ina,inb]でa~bのパス(aが上) vectorcomp;//連結成分の根 //inは頂点のindexを表す。また、edge列の下側の頂点である HLD(Graph &g,int r=-1):g(g),n(g.size()){ hld_build(r); } void hld_build(int root = -1){ in.assign(n,-1);out.assign(n,-1);dep.assign(n,0); par.assign(n,-1);head.assign(n,-1);sz.assign(n,-1);comp.assign(n,-1); edges.assign(n,edge()); if(root == -1){//根がどこでも良い場合(森でも可) for(int i=0;i sz[g[k][0].to])swap(e, g[k][0]); } } int time = 0; void dfs_hld(int k){ in[k] = time++; for(auto e:g[k]){ if(e.to == par[k])continue; head[e.to] = (e.to == g[k][0].to ? head[k]: e.to); edges[time] = e; dfs_hld(e.to); } out[k] = time; } int lca(int p,int q){ while(1){ if(in[p] < in[q])swap(p,q); if(head[p] == head[q])return q; p = par[head[p]]; } } vector>query_path(int p,int q,bool isEdge){ int r=lca(p,q); vector>ret; for(int i=0;i<2;i++){ if(i == 1)swap(p,q); while(1){ if(isEdge&&p==r)break; if(head[p]==head[r]){ ret.emplace_back(in[r]+(isEdge?1:i),in[p]+1); break; } ret.emplace_back(in[head[p]],in[p]+1); p = par[head[p]]; } } return ret; } vector>>query_order_path(int p,int q,bool isEdge){ //非可換クエリ用、配列0を順番を反転したデータ構造に、配列1を通常のデータ構造に vector>>ret(2); int r=lca(p,q); for(int i=0;i<2;i++){ if(i == 1)swap(p,q); while(1){ if(isEdge&&p==r)break; if(head[p]==head[r]){ if(i==0) ret[i].emplace_back(n-(in[p]+1),n-(in[r]+(isEdge?1:i))); else ret[i].emplace_back(in[r]+(isEdge?1:i),in[p]+1); break; } if(i==0) ret[i].emplace_back(n-(in[p]+1),n-(in[head[p]])); else ret[i].emplace_back(in[head[p]],in[p]+1); p = par[head[p]]; } } reverse(ret[1].begin(), ret[1].end()); return ret; } pairquery_subtree(int p,bool isEdge){ return make_pair(in[p]+isEdge,out[p]); } //uのv方向の子 子孫関係は前もって確認すること(in,outを見る) int child(int u,int v){ while(1){ if(head[u]==head[v]){ v=g[u][0].to; break; } v=head[v]; if(par[v]==u)break; v=par[v]; } return v; } long long dist(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)]; } }; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll res=0,buf=0; bool judge = true; int n,m;cin>>n>>m; auto g=readGraph(n,m); Lowlink low(g); low.makeBCT(); auto bct=low.bct_graph; auto idx=low.bct_index; ll sz=bct.size(); HLD hld(bct); vectora(sz+1); for(auto z:low.articulation)a[hld.in[idx[z]]+1]=1; rep(i,0,sz)a[i+1]+=a[i]; ll q;cin>>q; while(q--){ ll c,d;cin>>c>>d;c--;d--; ll ret=-(ll)low.art[c]-(ll)low.art[d]; auto tmp=hld.query_path(idx[c],idx[d],false); for(auto z:tmp){ ret+=a[z.se]-a[z.fi]; } cout<