//#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; template bool chmin(T &a,T b){if(a>b){a=b;return true;}else return false;} template bool chmax(T &a,T b){if(a void ans(bool x,T1 y,T2 z){if(x)cout< void debug(vector>&v,ll h,ll w){for(ll i=0;i&v,ll h,ll w){for(ll i=0;i void debug(vector&v,ll n){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,0,-1,0,1,1,-1,-1}; vectordy={0,1,0,-1,1,-1,1,-1}; template vector make_v(size_t a,T b){return vector(a,b);} template auto make_v(size_t a,Ts... ts){ return vector(a,make_v(ts...)); } template ostream &operator<<(ostream &os, const pair&p){ return os << p.first << " " << p.second; } struct Lowlink{ vector>G; int size; vectorord,low,used; vectorarticulation; vector>bridge; Lowlink(vector>&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;//二重辺連結成分の番号 vector>tecc_edges; 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; } } } } } vector>bct_edges; 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_edges.size(); bct_edges.emplace_back(); art[v] = true; } vectorcheck(G.size(),false); for(auto &ev:bcc){ int idx = bct_edges.size(); bct_edges.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_edges[idx].push_back(bct_index[v]); bct_edges[bct_index[v]].push_back(idx); } else if(!art[v]){ bct_index[v] = idx; } } } while(!rev.empty()){ check[rev.front()] = false; rev.pop(); } } } }; struct edge{ ll to=-1,cost=0; edge(){}; edge(ll t,ll c):to(t),cost(c){}; }; struct HLD{ ll n, root; ll cnt = 0; vectorsz;//部分木サイズ vectorpar,head; vector>g;//隣接リスト vectoredges;//データ構造に乗せるedge列 vectorin,out;//[in,out)で部分木、[ina,inb]でa~bのパス(aが上) //inは頂点のindexを表す。また、edge列の下側の頂点である HLD(vector>&G,ll r = -1) :g(G),n(G.size()),root(r){ par.assign(n,-1),in.assign(n,-1); out.assign(n,-1),head.assign(n,-1),sz.assign(n,-1); edges.assign(n,edge()); dfs_build(); } void dfs_build(){ if(root == -1){//根がどこでも良い場合(森でも可) for(ll i=0;i sz[g[k][0].to])swap(e, g[k][0]); } } void dfs_hld(ll k){ in[k] = cnt++; 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[cnt] = e; dfs_hld(e.to); } out[k] = cnt; } ll lca(ll p,ll 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(ll p,ll q,bool isEdge){ ll r=lca(p,q); vector>ret; for(ll 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(ll p,ll q,bool isEdge){ //非可換クエリ用、配列0を順番を反転したデータ構造に、配列1を通常のデータ構造に vector>>ret(2); ll r=lca(p,q); for(ll 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(ll p,bool isEdge){ return make_pair(in[p]+isEdge,out[p]); } //uのv方向の子 子孫関係は前もって確認すること ll child(ll u,ll 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; } }; 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; vector>g(n); rep(i,0,m){ int u,v;cin>>u>>v;u--;v--; g[u].PB(v); g[v].PB(u); } Lowlink low(g); low.makeBCT(); auto bct=low.bct_edges; auto idx=low.bct_index; ll sz=bct.size(); vector>gh(sz); //debug(idx,n); rep(i,0,sz){ for(auto to:bct[i]){ //cout<a(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<