#include #include #define rep(i,b) for(int i=0;i=0;i--) #define rep1(i,b) for(int i=1;i=x;i--) #define fore(i,a) for(auto& i:a) #define rng(x) (x).begin(), (x).end() #define rrng(x) (x).rbegin(), (x).rend() #define sz(x) ((int)(x).size()) #define pb push_back #define fi first #define se second #define pcnt __builtin_popcountll using namespace std; using namespace atcoder; using ll = long long; using ull = long long; using ld = long double; template using mpq = priority_queue, greater>; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (b ll sumv(const vector&a){ll res(0);for(auto&&x:a)res+=x;return res;} bool yn(bool a) { if(a) {cout << "Yes" << endl; return true;} else {cout << "No" << endl; return false;}} #define retval(x) {cout << #x << endl; return;} #define cout2(x,y) cout << x << " " << y << endl; #define coutp(p) cout << p.fi << " " << p.se << endl; #define out cout << ans << endl; #define outd cout << fixed << setprecision(20) << ans << endl; #define outm cout << ans.val() << endl; #define outv fore(yans , ans) cout << yans << "\n"; #define outdv fore(yans , ans) cout << yans.val() << "\n"; #define assertmle(x) if (!(x)) {vi v(3e8);} #define asserttle(x) if (!(x)) {while(1){}} #define coutv(v) {fore(vy , v) {cout << vy << " ";} cout << endl;} #define coutv2(v) fore(vy , v) cout << vy << "\n"; #define coutvm(v) {fore(vy , v) {cout << vy.val() << " ";} cout << endl;} #define coutvm2(v) fore(vy , v) cout << vy.val() << "\n"; using pll = pair;using pil = pair;using pli = pair;using pii = pair;using pdd = pair; using vi = vector;using vd = vector;using vl = vector;using vs = vector;using vb = vector; using vpii = vector;using vpli = vector;using vpll = vector;using vpil = vector; using vvi = vector>;using vvl = vector>;using vvs = vector>;using vvb = vector>; using vvpii = vector>;using vvpli = vector>;using vvpll = vector;using vvpil = vector; using mint = modint998244353; //using mint = modint1000000007; //using mint = dynamic_modint<0>; using vm = vector; using vvm = vector>; vector dx={1,0,-1,0,1,1,-1,-1},dy={0,1,0,-1,1,-1,1,-1}; ll gcd(ll a, ll b) { return a?gcd(b%a,a):b;} ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} #define yes {cout <<"Yes"< struct LCA { int n, root, l; vector> to; vector> co; vector dep; vector costs; vector> par; dsu d; vi r; LCA(int n):n(n),to(n),co(n),dep(n),costs(n) { l = 0; while ((1<>(n+1,vector(l,n)); d = dsu(n); r = vi(n,-1); } void add_edge(int a, int b, T c=1) { if (a==b) return; to[a].push_back(b); co[a].push_back(c); to[b].push_back(a); co[b].push_back(c); if (a!=n && b!=n){ d.merge(a,b); } } // par[x][0],dep,costsを計算 void _dfs(int v, int d=0, T c=0, int p=-1, int now=-1) { if (p != -1) par[v][0] = p; if (now==-1 && v!=root) now = v; r[v] = now; dep[v] = d; costs[v] = c; for (int i = 0; i < sz(to[v]); ++i) { int u = to[v][i]; if (u == p) continue; _dfs(u, d+1, c+co[v][i], v, now); } } void init(int _root=0) { root = _root; _dfs(root); for (int i = 0; i < l-1; ++i) { for (int v = 0; v < n; ++v) { par[v][i+1] = par[par[v][i]][i]; } } } int lca(int a,int b){ if (!d.same(a,b)) return -1; if (dep[a] > dep[b]) swap(a,b); int gap = dep[b]-dep[a]; for (int i = l-1; i >= 0; --i) { int len = 1<= len) { gap -= len; b = par[b][i]; } } if (a == b) return a; for (int i = l-1; i >= 0; --i) { int na = par[a][i]; int nb = par[b][i]; if (na != nb) a = na, b = nb; } return par[a][0]; } int getPar(int x,int k){ int now = x; rrep(i,30){ if ((k >> i & 1) == 1){ now = par[now][i]; } } return now; } int length(int a, int b) { if (!d.same(a,b)) return -1; int c = lca(a,b); return dep[a]+dep[b]-dep[c]*2; } T dist(int a, int b) { int c = lca(a,b); return costs[a]+costs[b]-costs[c]*2; } }; // 初期化方法 LCA g(n) : nは頂点数、Tはコストのタイプ、注) 宣言 → add_edge → init の順で呼び出す // add_edge(a,b,c) : a,b間にコストcの辺をはる。cは省略可(1となる)。 // init() : ダブリングにより各頂点の2^i個上の親を計算。結果はpar[v][i]に入る。 // lca(a,b) : 頂点a,bのlcaを出力。 // getPar(x,k) : 頂点xの2^k上の祖先を出力。 // length(a,b) : 頂点a,b間の辺の数を出力。 // dist(a,b) : 頂点a,b間のコストを出力。 void solve(){ int n,l,r; cin>>n>>l>>r; vi a(n); rep(i,n) cin>>a[i],a[i]--; vi d(n); vvi e(n); dsu ds(n); rep(i,n){ d[a[i]]++; e[i].pb(a[i]); ds.merge(i, a[i]); } queue q; rep(i,n) if (d[i]==0) q.push(i); vb flg(n,true); while(sz(q)){ int p = q.front(); q.pop(); flg[p] = false; fore(y , e[p]){ if (--d[y]==0) q.push(y); } } int root = n; LCA lc(n+1); rep(i,n){ if (flg[i]){ lc.add_edge(i, root); continue; } fore(y , e[i]){ lc.add_edge(i, y); } } lc.init(root); vvi loop; vb flg2(n); vi id(n,-1); vi id_loop(n,-1); rep(i,n){ if (!flg[i]) continue; if (flg2[i]) continue; int x = a[i]; vi v = {i}; flg2[i] = true; while(x != i){ v.pb(x); flg2[x] = true; x = a[x]; } rep(j,sz(v)){ id[v[j]] = j; id_loop[v[j]] = sz(loop); } loop.pb(v); } auto get_loop_size = [&](int i)->int{ assertmle(flg[i]); return sz(loop[id_loop[i]]); }; // 同じループにある頂点通しの距離 auto get_loop_dist = [&](int a,int b)->int{ asserttle(flg[a]); asserttle(flg[b]); asserttle(ds.same(a,b)); int ret; int ai = id[a]; int bi = id[b]; if (ai <= bi) ret = bi-ai; else{ ret = get_loop_size(a) - (ai-bi); } return ret; }; auto get_dist = [&](int a,int b)->int{ if (!ds.same(a,b)) return -1; if (!flg[a] && !flg[b] && !lc.d.same(a,b)) return -1; if (flg[a] && !flg[b]) return -1; if (flg[a] && flg[b]){ return get_loop_dist(a,b); }else if(!flg[a] && flg[b]){ int ar = lc.r[a]; return lc.dist(a,ar) + get_loop_dist(ar,b); }else{ int c = lc.lca(a,b); if (c==b) return lc.dist(a,b); else return -1; } }; int qi; cin>>qi; vi ans(qi,-1); rep(i,qi){ int a,b; cin>>a>>b; a--; b--; int dist = get_dist(a,b); if (dist==-1) continue; if (!flg[b]){ int k = (dist+r-1)/r; if (k*l <= dist) ans[i] = k; continue; } ll d1 = dist; ll d2 = get_loop_size(b); auto get_sum = [&](ll x, ll cnt, bool isR)->ll{ if (x==0) return 0; ll lower = (d1+!isR+x-1)/x; if (lower > cnt) return 0; ll ret = floor_sum(cnt+1,d2,x,(d2-d1-!isR)); // assert(ret>=0); ret -= floor_sum(lower,d2,x,(d2-d1-!isR)); return ret; }; auto check = [&](ll mid)->bool{ return (get_sum(r,mid,true)==get_sum(l,mid,false)); }; ll li=0,ui=n+1; if (check(ui)) continue; while(ui-li>1){ ll mid = (li+ui)/2; if (check(mid)) li = mid; else ui = mid; } ans[i] = ui; } outv; return; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int t = 1; //cin>>t; rep(i,t){ solve(); } return 0; }