結果

問題 No.1326 ふたりのDominator
ユーザー hotman78hotman78
提出日時 2020-12-23 02:29:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 18,367 bytes
コンパイル時間 6,022 ms
コンパイル使用メモリ 291,316 KB
実行使用メモリ 814,848 KB
最終ジャッジ日時 2024-09-23 03:00:58
合計ジャッジ時間 11,211 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 187 ms
19,984 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 MLE -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
code.cpp:64:9: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:64:16: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:75:15: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:84:23: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:91:22: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:118:16: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:122:16: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:122:30: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:130:18: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:140:10: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:140:17: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:147:21: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:150:8: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:150:15: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
code.cpp:164:23: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'

ソースコード

diff #

#line 2 "cpplib/util/template.hpp"
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
struct __INIT__{__INIT__(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}__INIT__;
typedef long long lint;
#define INF (1LL<<60)
#define IINF (1<<30)
#define EPS (1e-10)
#define endl ('\n')
typedef vector<lint> vec;
typedef vector<vector<lint>> mat;
typedef vector<vector<vector<lint>>> mat3;
typedef vector<string> svec;
typedef vector<vector<string>> smat;
template<typename T>using V=vector<T>;
template<typename T>using VV=V<V<T>>;
template<typename T>inline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i;f=1;}cout<<endl;}
template<typename T>inline void output2(T t){for(auto i:t)output(i);}
template<typename T>inline void debug(T t){bool f=0;for(auto i:t){cerr<<(f?" ":"")<<i;f=1;}cerr<<endl;}
template<typename T>inline void debug2(T t){for(auto i:t)output(i);}
#define loop(n) for(long long _=0;_<(long long)(n);++_)
#define _overload4(_1,_2,_3,_4,name,...) name
#define __rep(i,a) repi(i,0,a,1)
#define _rep(i,a,b) repi(i,a,b,1)
#define repi(i,a,b,c) for(long long i=(long long)(a);i<(long long)(b);i+=c)
#define rep(...) _overload4(__VA_ARGS__,repi,_rep,__rep)(__VA_ARGS__)
#define _overload3_rev(_1,_2,_3,name,...) name
#define _rep_rev(i,a) repi_rev(i,0,a)
#define repi_rev(i,a,b) for(long long i=(long long)(b)-1;i>=(long long)(a);--i)
#define rrep(...) _overload3_rev(__VA_ARGS__,repi_rev,_rep_rev)(__VA_ARGS__)

// #define rep(i,...) for(auto i:range(__VA_ARGS__)) 
// #define rrep(i,...) for(auto i:reversed(range(__VA_ARGS__)))
// #define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i)
// #define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i)
// #define irep(i) for(lint i=0;;++i)
// inline vector<long long> range(long long n){if(n<=0)return vector<long long>();vector<long long>v(n);iota(v.begin(),v.end(),0LL);return v;}
// inline vector<long long> range(long long a,long long b){if(b<=a)return vector<long long>();vector<long long>v(b-a);iota(v.begin(),v.end(),a);return v;}
// inline vector<long long> range(long long a,long long b,long long c){if((b-a+c-1)/c<=0)return vector<long long>();vector<long long>v((b-a+c-1)/c);for(int i=0;i<(int)v.size();++i)v[i]=i?v[i-1]+c:a;return v;}
// template<typename T>inline T reversed(T v){reverse(v.begin(),v.end());return v;}
#define all(n) begin(n),end(n)
template<typename T,typename E>bool chmin(T& s,const E& t){bool res=s>t;s=min<T>(s,t);return res;}
template<typename T,typename E>bool chmax(T& s,const E& t){bool res=s<t;s=max<T>(s,t);return res;}
const vector<lint> dx={1,0,-1,0,1,1,-1,-1};
const vector<lint> dy={0,1,0,-1,1,-1,1,-1};
#define SUM(v) accumulate(all(v),0LL)
template<typename T,typename ...Args>auto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector<T>(arg,x);else return vector(arg,make_vector<T>(x,args...));}
#define extrep(v,...) for(auto v:__MAKE_MAT__({__VA_ARGS__}))
vector<vector<long long>> __MAKE_MAT__(vector<long long> v){if(v.empty())return vector<vector<long long>>(1,vector<long long>());long long n=v.back();v.pop_back();vector<vector<long long>> ret;vector<vector<long long>> tmp=__MAKE_MAT__(v);for(auto e:tmp)for(long long i=0;i<n;++i){ret.push_back(e);ret.back().push_back(i);}return ret;}
using graph=vector<vector<int>>;
template<typename T>using graph_w=vector<vector<pair<int,T>>>;
template<typename T,typename E>ostream& operator<<(ostream& out,pair<T,E>v){out<<"("<<v.first<<","<<v.second<<")";return out;}
constexpr inline long long powll(long long a,long long b){long long res=1;while(b--)res*=a;return res;}
#line 1 "cpplib/dsu/union_find.hpp"
/**
 * @brief Union Find
 * @see https://ja.wikipedia.org/wiki/%E7%B4%A0%E9%9B%86%E5%90%88%E3%83%87%E3%83%BC%E3%82%BF%E6%A7%8B%E9%80%A0
 */

class UF{
    public:
    vector<int> data;
    int sz;
	public:
    UF(int sz):sz(sz){data.resize(sz,-1);}
    bool unite(int x,int y){
        x=root(x);y=root(y);
        if(x==y)return 0;
        if(data[x]>data[y])swap(x,y);
		data[x]+=data[y];
		data[y]=x;
		sz--;
        return 1;
    }
    inline int root(int x){return data[x]<0?x:data[x]=root(data[x]);}
    inline bool same(int x, int y){return root(x)==root(y);}
    inline int size(){return sz;}
	inline int size(int x){return -data[root(x)];}
};
#line 5 "cpplib/graph_tree/graph_template.hpp"
/**
 * @brief グラフテンプレート
 */

using graph=std::vector<std::vector<int>>;
template<typename T>
using graph_w=std::vector<std::vector<std::pair<int,T>>>;

graph load_graph(int n,int m){graph g(n);for(int i=0;i<m;++i){int s,t;std::cin>>s>>t;--s;--t;g[s].push_back(t);g[t].push_back(s);}return g;}
graph load_digraph(int n,int m){graph g(n);for(int i=0;i<m;++i){int s,t;std::cin>>s>>t;--s;--t;g[s].push_back(t);}return g;}
graph load_graph0(int n,int m){graph g(n);for(int i=0;i<m;++i){int s,t;std::cin>>s>>t;g[s].push_back(t);g[t].push_back(s);}return g;}
graph load_digraph0(int n,int m){graph g(n);for(int i=0;i<m;++i){int s,t;std::cin>>s>>t;g[s].push_back(t);}return g;}
graph load_tree(int n){graph g(n);for(int i=0;i<n-1;++i){int s,t;std::cin>>s>>t;--s;--t;g[s].push_back(t);g[t].push_back(s);}return g;}
graph load_tree0(int n){graph g(n);for(int i=0;i<n-1;++i){int s,t;std::cin>>s>>t;g[s].push_back(t);g[t].push_back(s);}return g;}
graph load_treep(int n){graph g(n);for(int i=0;i<n-1;++i){int t;std::cin>>t;g[i+1].push_back(t);g[t].push_back(i+1);}return g;}
template<typename T>graph_w<T> load_graph_weight(int n,int m){graph_w<T> g(n);for(int i=0;i<m;++i){int s,t;T u;std::cin>>s>>t>>u;--s;--t;g[s].emplace_back(t,u);g[t].emplace_back(s,u);}return g;}
template<typename T>graph_w<T> load_digraph_weight(int n,int m){graph_w<T> g(n);for(int i=0;i<m;++i){int s,t;T u;std::cin>>s>>t>>u;--s;--t;g[s].emplace_back(t,u);}return g;}
template<typename T>graph_w<T> load_graph0_weight(int n,int m){graph_w<T> g(n);for(int i=0;i<m;++i){int s,t;T u;std::cin>>s>>t>>u;g[s].emplace_back(t,u);g[t].emplace_back(s,u);}return g;}
template<typename T>graph_w<T> load_digraph0_weight(int n,int m){graph_w<T> g(n);for(int i=0;i<m;++i){int s,t;T u;std::cin>>s>>t>>u;g[s].emplace_back(t,u);}return g;}
template<typename T>graph_w<T> load_tree_weight(int n){graph_w<T> g(n);for(int i=0;i<n-1;++i){int s,t;T u;std::cin>>s>>t>>u;--s;--t;g[s].emplace_back(t,u);g[t].emplace_back(s,u);}return g;}
template<typename T>graph_w<T> load_tree0_weight(int n){graph_w<T> g(n);for(int i=0;i<n-1;++i){int s,t;T u;std::cin>>s>>t>>u;g[s].emplace_back(t,u);g[t].emplace_back(s,u);}return g;}
template<typename T>graph_w<T> load_treep_weight(int n){graph_w<T> g(n);for(int i=0;i<n-1;++i){int t;T u;std::cin>>t>>u;g[i+1].emplace_back(t,u);g[t].emplace_back(i+1,u);}return g;}
#line 5 "cpplib/graph_tree/depth.hpp"

/**
 * @brief 根からの深さ
 */

std::vector<int> depth(const graph& g,int start){
	std::vector<int>memo(g.size());
	auto f=[&](auto f,int v,int p)->int{
		int mx=0;
		for(auto t:g[v]){
			if(t==p)continue;
			mx=std::max(mx,f(f,t,v));
		}
		return memo[v]=mx+1;
	};
	f(f,start,-1);
	return memo;
}
#line 1 "cpplib/graph_tree/distance.hpp"
std::vector<int> distance(const graph& G,int start){
    std::vector<int>memo(G.size());
    auto f=[&](auto f,int v,int p,int i)->void{
        for(auto t:G[v]){
            if(t==p)continue;
            f(f,t,v,i+1);
        }
        memo[v]=i;
    };
    f(f,start,-1,0);
    return memo;
}
#line 3 "cpplib/data_structure/small_rmq.hpp"

/**
 * RMQ(small) &amp;lt;O(N),O(1)&amp;gt;(N<=64)
 */

template<typename T>
class small_rmq{
    using u64=unsigned long long;
    std::vector<u64>table;
    std::vector<T> v;
    public:
    small_rmq(std::vector<T> v):v(v){
        assert(v.size()<=64);
        std::vector<int>tmp(v.size());
        table.resize(v.size(),0);
        std::stack<T>stk;
        for(int i=0;i<(int)v.size();++i){
            tmp.resize(v.size());
            while(!stk.empty()&&v[stk.top()]>=v[i]){
                stk.pop();
            }
            tmp[i]=stk.empty()?-1:stk.top();
            stk.emplace(i);
        }
        for(int i=0;i<(int)v.size();++i){
            if(tmp[i]!=-1)table[i]=table[tmp[i]]|(1ULL<<(tmp[i]));
        }
    }
    T query(int l,int r){
        assert(l!=r);
        const u64 tmp=table[r-1]&~((1ULL<<l)-1);
        if(tmp==0)return r-1;
        else return __builtin_ctzll(tmp);
    }
};
#line 3 "cpplib/alga/maybe.hpp"

/**
 * @brief Maybe
 * @see https://ja.wikipedia.org/wiki/%E3%83%A2%E3%83%8A%E3%83%89_(%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0)#Maybe%E3%83%A2%E3%83%8A%E3%83%89
 */

template<typename T>
struct maybe{
    bool _is_none;
    T val;
    maybe():_is_none(true){}
    maybe(T val):_is_none(false),val(val){}
    T unwrap()const{
        assert(!_is_none);
        return val;
    }
    T unwrap_or(T e)const{
        return _is_none?e:val;
    }
    bool is_none()const{return _is_none;}
    bool is_some()const{return !_is_none;}
};

template<typename T,typename F>
auto expand(F op){
    return [&op](const maybe<T>& s,const maybe<T>& t){
        if(s.is_none())return t;
        if(t.is_none())return s;
        return maybe<T>(op(s.unwrap(),t.unwrap()));
    };
}
#line 7 "cpplib/data_structure/sparse_table.hpp"
/**
 * @brief SparseTable
 */

template<typename T,typename F>
class sparse_table{
    F f;
    std::vector<std::vector<T>>data;
    public:
    sparse_table(std::vector<T> v,F f=F()):f(f){
        int n=v.size(),log=log2(n)+1;
        data.resize(n,std::vector<T>(log));
        for(int i=0;i<n;i++)data[i][0]=v[i];
        for(int j=1;j<log;j++)for(int i=0;i+(1<<(j-1))<n;i++){
            data[i][j]=f(data[i][j-1],data[i+(1<<(j-1))][j-1]);
        }
    }
    maybe<T> get(int l,int r){
        if(l==r)return maybe<T>();
        if(r<l)std::swap(l,r);
        int k=std::log2(r-l);
        return maybe<T>(f(data[l][k],data[r-(1<<k)][k]));
    }
};
#line 3 "cpplib/functional/argmin.hpp"

/**
 * @brief 最小値とその位置
 */

template<typename T,typename E>
struct argmin{
    std::pair<T,E> operator()(const std::pair<T,E>& s,const std::pair<T,E>& t){
        return s.second<=t.second?s:t;
    }
};
#line 11 "cpplib/data_structure/arg_rmq.hpp"

/**
 * @brief RangeArgminQuery &amp;lt;O(N),O(1)&amp;gt;
 * @see https://noshi91.hatenablog.com/entry/2018/08/16/125415
 */

template<typename T>
class arg_rmq{
    constexpr static int b=64;
    std::vector<T>v;
    std::vector<small_rmq<T>*>backet;
    sparse_table<std::pair<int,T>,argmin<int,T>>* st=0;
    public:
    arg_rmq(std::vector<T>v):v(v){
        std::vector<std::pair<int,T>>tmp2;
        for(int i=0;i<(int)v.size();i+=b){
            std::vector<T>tmp;
            T mn=std::numeric_limits<T>::max();
            int idx=-1;
            for(int j=0;i+j<(int)v.size()&&j<b;j++){
                tmp.push_back(v[i+j]);
                if(mn>v[i+j]){
                    mn=v[i+j];
                    idx=i+j;
                }
            }
            tmp2.emplace_back(idx,mn);
            backet.push_back(new small_rmq<T>(tmp));
        }
        st=new sparse_table<std::pair<int,T>,argmin<int,T>>(tmp2);
    }
    maybe<std::pair<int,T>> query(int s,int t){
        if(s==t)return maybe<std::pair<int,T>>();
        if(s/b==t/b){
            int idx=s/b*b+backet[s/b]->query(s%b,t%b);
            return maybe<std::pair<int,T>>(std::make_pair(idx,v[idx]));
        }
        std::pair<int,T> res=std::make_pair(-1,std::numeric_limits<T>::max());

        {
            int idx=s/b*b+backet[s/b]->query(s%b,b);
            res=argmin<int,T>()(res,std::make_pair(idx,v[idx]));
        }

        if(s/b+1!=t/b)res=argmin<int,T>()(res,st->get(s/b+1,t/b).unwrap());

        if(t%b!=0){
            int idx=t/b*b+backet[t/b]->query(0,t%b);
            res=argmin<int,T>()(res,std::make_pair(idx,v[idx]));
        }

        return maybe<std::pair<int,T>>(res);
    }
};
#line 9 "cpplib/graph_tree/lca.hpp"

/**
 * @brief LCA &amp;lt;O(N),O(1)&amp;gt;(HL分解と同等の速さ)
 */

class lca{
    std::vector<int>data;
    std::vector<int>comp_data;
    std::vector<int>start;
    arg_rmq<int>*st;
    std::vector<int> __dist;
    public:
    lca(){}
    lca(std::vector<std::vector<int>>v,int s){
        data.resize(v.size()*2-1);
        comp_data.resize(v.size()*2-1);
        start.resize(v.size());
        int i=0;
        __dist=distance(v,s);
        auto f=[&](auto f,int n,int p)->void{
            start[n]=i;
            data[i]=n;
            comp_data[i++]=__dist[n];
            for(int t:v[n]){
                if(t==p)continue;
                f(f,t,n);
                data[i]=n;
                comp_data[i++]=__dist[n];
            }
        };
        f(f,s,-1);
        st=new arg_rmq<int>(comp_data);
    }
    int query(int p,int q){
        return data[st->query(std::min(start[p],start[q]),std::max(start[p],start[q])+1).unwrap().first];
    }
    int dist(int p,int q){
        return __dist[p]+__dist[q]-2*__dist[query(p,q)];
    }
};
#line 4 "code.cpp"

template< typename G >
struct LowLink {
	const G &g;
	vector< int > used, ord, low;
	vector< int > articulation;
	vector< pair< int, int > > bridge;
	
	LowLink(const G &g) : g(g) {}
	
	int dfs(int idx, int k, int par) {
		used[idx] = true;
		ord[idx] = k++;
		low[idx] = ord[idx];
		bool is_articulation = false;
		int cnt = 0;
		for(auto &to : g[idx]) {
			if(!used[to]) {
				++cnt;
				k = dfs(to, k, idx);
				low[idx] = min(low[idx], low[to]);
				is_articulation |= ~par && low[to] >= ord[idx];
				if(ord[idx] < low[to]) bridge.emplace_back(minmax(idx, (int) to));
			} else if(to != par) {
				low[idx] = min(low[idx], ord[to]);
			}
		}
		is_articulation |= par == -1 && cnt > 1;
		if(is_articulation) articulation.push_back(idx);
		return k;
	}
	
	virtual void build() {
		used.assign(g.size(), 0);
		ord.assign(g.size(), 0);
		low.assign(g.size(), 0);
		int k = 0;
		for(int i = 0; i < (int)g.size(); i++) {
			if(!used[i]) k = dfs(i, k, -1);
		}
	}
};

template<typename T>
class HLD_seg{
	class segment{
		T* node;
		int n=1;
		public:
		segment(int sz){
			while(n<sz)n<<=1;
			node=new T[(n<<1)-1];
			for(int i=0;i<(n<<1)-1;i++)node[i]=e;
		}
		segment(const vector<T>& v){
			while(n<(int)v.size())n<<=1;
			node=new T[(n<<1)-1];
			for(int i=0;i<(int)v.size();i++)node[i+n-1]=v[i];
        	for(int i=n-2;i>=0;i--)node[i]=f(node[(i<<1)+1],node[(i<<1)+2]);
		}
		T get(auto l,auto r){
			if(l>r)swap(l,r);
			l+=n;r+=n+1;
			T s=e;
			while(l<r){
				if(l&1)s=f(s,node[l++-1]);
				if(r&1)s=f(s,node[--r-1]);
				l>>=1;r>>=1;
			}
			return s;
		}
		void update(auto t,T val){
			t+=n-1;
			while(1){
				node[t]=f(node[t],val);
				t=(t-1)>>1;
				if(!t)break;
			}
		}
	};
	int child_size(const auto& v,int n,int p){
		int cnt=0;
		for(auto t:v[n]){
			if(t!=p)cnt+=child_size(v,t,n);
		}
		return sz[n]=cnt+1;
	}
	void make_hld(const auto&v,int n,int p){
		static int idx=0;
		vertex[n]=idx++;
		int mx=0,heavy=-1;
		for(auto t:v[n])if(t!=p&&mx<sz[t]){
			mx=sz[t];
			heavy=t;
		}
		if(heavy!=-1){
			par[heavy]=par[n];
			head[heavy]=head[n];
			make_hld(v,heavy,n);
		}
		for(auto t:v[n]){
			if(t!=heavy&&t!=p){
				par[t]=n;
				head[t]=t;
				make_hld(v,t,n);
			}
		}
	}
	int* sz;
	int* vertex;
	int* par;
	int* head;
	segment* seg;
	public:
	HLD_seg(const auto& v,int root=0){
		make(v,root);
		seg=new segment(v.size());
	}
	HLD_seg(const auto& v,const auto& a,int root=0){
		make(v,root);
		vector<T>tmp(v.size());
		for(int i=0;i<(int)v.size();i++){
			tmp[vertex[i]]=a[i];
		}
		seg=new segment(tmp);
	}
	void make(const auto& v,int root){
		sz=new int[v.size()];
		vertex=new int[v.size()];
		par=new int[v.size()];
		head=new int[v.size()];
		child_size(v,root,-1);
		par[root]=root;
		head[root]=root;
		make_hld(v,root,-1);
	}
	int lca(auto l,auto r){
		while(head[l]!=head[r]){
			if(sz[head[l]]>sz[head[r]])r=par[r];
			else l=par[l];
		}
		return sz[l]>sz[r]?l:r;
	}
	inline void update(auto t,T x){
		seg->update(vertex[t],x);
	}
	T get(auto u,auto v){
		T res=e;
		while(head[u]!=head[v]){
			if(sz[head[u]]>sz[head[v]]){
				res=f(res,seg->get(vertex[v],vertex[head[v]]));
				v=par[v];
			}
			else{
				res=f(res,seg->get(vertex[u],vertex[head[u]]));
				u=par[u];
			}
		}
		return f(res,seg->get(vertex[u],vertex[v]));
	}
	inline T get_subtree(auto u){
		return seg->get(vertex[u],vertex[u]+sz[u]-1);
	}
	protected:
	constexpr static T e=0;
	constexpr static
	T f(const T& s,const T& t){
		return s+t;
	}
};

int main(){
	lint n,m;
	cin>>n>>m;
	mat g(n);
	rep(i,m){
		lint s,t;
		cin>>s>>t;
		s--;t--;
		g[s].emplace_back(t);
		g[t].emplace_back(s);
	}
	LowLink<mat>t(g);
	t.build();
	UF uf(n);
	auto v=t.articulation;
	sort(all(v));
	rep(i,n){
		if(binary_search(all(v),i))continue;
		for(auto e:g[i]){
			if(!binary_search(all(v),e)){
				uf.unite(i,e);
			}
		}
	}
	map<pair<lint,lint>,lint>m2;
	lint idx=0;
	vector<lint>mm(n);
	rep(i,n){
		if(uf.root(i)==i){
			mm[i]=idx++;
		}
	}
	mat g2(idx);
	rep(i,n){
		if(!binary_search(all(v),i))continue;
		for(auto e:g[i]){
			g2[mm[uf.root(i)]].emplace_back(mm[uf.root(e)]);
			g2[mm[uf.root(e)]].emplace_back(mm[uf.root(i)]);
		}
	}
	rep(i,idx){
		sort(all(g2[i]));
		g2[i].erase(unique(all(g2[i])),g2[i].end());
	}
	LowLink<mat>t2(g2);
	t2.build();
	UF uf2(idx);
	auto b=t2.bridge;
	sort(all(b));
	//output(b);
	for(int i:v){
		vec v;
		for(int e:g2[mm[uf.root(i)]]){
			if(!binary_search(all(b),(pair<int,int>)minmax((int)mm[uf.root(i)],e))){
				v.push_back(e);
			}
		}
		rep(j,v.size()-1){
			uf2.unite(v[j],v[j+1]);
		}
	}
	lint idx2=0;
	vector<lint>mm2(idx);
	rep(i,idx){
		if(uf2.root(i)==i){
			mm2[i]=idx2++;
		}
	}
	mat g3(idx2);
	rep(i,idx){
		for(auto e:g2[i]){
			if(uf2.same(i,e))continue;
			g3[mm2[uf2.root(i)]].push_back(mm2[uf2.root(e)]);
			g3[mm2[uf2.root(e)]].push_back(mm2[uf2.root(i)]);
		}
	}
	rep(i,idx2){
		sort(all(g3[i]));
		g3[i].erase(unique(all(g3[i])),g3[i].end());
	}
	// rep(i,n){
	// 	cout<<mm2[uf2.root(mm[uf.root(i)])]<<" \n"[i==n-1];
	// }
	vector<lint>vv(idx2,0);
	for(auto e:v){
		vv[mm2[uf2.root(mm[e])]]=1;
	}
	//output2(g3);
	HLD_seg<lint> hld(g3,vv,0);
	lint q;
	cin>>q;
	rep(i,q){
		lint s,t;
		cin>>s>>t;
		s--;t--;
		if(s==t)cout<<0<<endl;
		else cout<<hld.get(mm2[uf2.root(mm[uf.root(s)])],mm2[uf2.root(mm[uf.root(t)])])-binary_search(all(v),s)-binary_search(all(v),t)<<endl;
	}
}
0