In the NN country, there are n
roads, connecting them. There is a roads path between any two cities.
There are m
bidirectional bus routes between cities. Buses drive between two cities taking the shortest path with stops in every city they drive through. Travelling by bus, you can travel from any stop on the route to any other. You can travel between cities only by bus.
You are interested in q
questions: is it possible to get from one city to another and what is the minimum number of buses you need to use for it?
The first line contains a single integer n
) — the number of cities.
The second line contains n−1
integers p2,p3,…,pn ( 1≤pi<i), where pi means that cities pi and iare connected by road.
The third line contains a single integer m
( 1≤m≤2⋅105) — the number of bus routes.
Each of the next m
lines contains 2 integers a and b ( 1≤a,b≤n, a≠b), meaning that there is a bus route between cities a and b. It is possible that there is more than one route between two cities.
The next line contains a single integer q
( 1≤q≤2⋅105) — the number of questions you are interested in.
Each of the next q
lines contains 2 integers v and u ( 1≤v,u≤n, v≠u), meaning that you are interested if it is possible to get from city v to city uand what is the minimum number of buses you need to use for it.
Print the answer for each question on a separate line. If there is no way to get from one city to another, print −1
. Otherwise print the minimum number of buses you have to use.
7 1 1 1 4 5 6 4 4 2 5 4 1 3 6 7 6 4 5 3 5 7 2 4 5 3 2 5 3
1 3 -1 1 2 3
7 1 1 2 3 4 1 4 4 7 3 5 7 6 7 6 6 4 6 3 1 3 2 2 7 6 3 5 3
1 -1 -1 1 -1 1
#define happy #include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define all(a) (a).begin(),(a).end() #define pll pair<ll,ll> #define vi vector<int> #define pb push_back const int inf=0x3f3f3f3f; ll rd(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } const int MN=2e5; const int K=18; int en,h[MN+5],l[MN+5],r[MN+5],cnt,fa[K][MN+5],u[K][MN+5]; int A[MN+5],B[MN+5],a[MN+5],b[MN+5],c[MN+5],s[MN+5],t[MN+5],d[MN+5]; vector<int> v[MN+5]; struct edge{ int nx,t; }e[MN*2+5]; inline void ins(int x,int y){ e[++en]=(edge){h[x],y};h[x]=en; e[++en]=(edge){h[y],x};h[y]=en; } void add(int x){ for(;x<=MN;x+=x&-x)++t[x]; } int sum(int x){ int res=0; for(;x;x-=x&-x)res+=t[x];return res; } void dfs(int x){ l[x]=++cnt; for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa[0][x]) fa[0][e[i].t]=x,u[0][e[i].t]=d[e[i].t]=d[x]+1,dfs(e[i].t); r[x]=cnt; } void dp(int x){ for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa[0][x]) dp(e[i].t),u[0][x]=min(u[0][x],u[0][e[i].t]); } int up(int x,int y){ for(int i=0;y;y>>=1,i++)if(y&1)x=fa[i][x]; return x; } int lca(int x,int y){ int k=d[x]-d[y],i; if(k<0)swap(x,y),k=-k; for(i=0;k;k>>=1,i++)if(k&1)x=fa[i][x]; if(x==y)return x; for(i=K;i--;)if(fa[i][x]!=fa[i][y])x=fa[i][x],y=fa[i][y]; return fa[0][x]; } void solve(int x){ for(auto uu:v[x]) if(uu>0)s[uu]-=sum(r[b[uu]])-sum(l[b[uu]]-1); for(auto uu:v[x]) if(uu<0)add(l[-uu]); for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa[0][x])solve(e[i].t); for(auto uu:v[x]) if(uu>0)s[uu]+=sum(r[b[uu]])-sum(l[b[uu]]-1); } int main(){ #ifdef happy freopen("in.txt","r",stdin); #endif int n=rd(); rep(i,2,n)ins(rd(),i); dfs(1); rep(l,1,K-1)rep(i,1,n)fa[l][i]=fa[l-1][fa[l-1][i]]; //倍增lca int t=rd(); rep(i,1,t){ int l=lca(A[i]=rd(),B[i]=rd()); u[0][A[i]]=min(u[0][A[i]],d[l]); u[0][B[i]]=min(u[0][B[i]],d[l]); v[A[i]].push_back(-B[i]); v[B[i]].push_back(-A[i]); } dp(1); //转成节点 rep(i,1,n)u[0][i]=up(i,d[i]-u[0][i]); rep(l,1,K-1)rep(i,1,n)u[l][i]=u[l-1][u[l-1][i]]; int m=rd(); rep(i,1,m){ int l=lca(a[i]=rd(),b[i]=rd()); if(b[i]==l)swap(a[i],b[i]); if(a[i]==l){ for(int j=K;j--;)if(d[u[j][b[i]]]>d[l])b[i]=u[j][b[i]],c[i]+=1<<j; if(d[u[0][b[i]]]>d[l])c[i]=-1; continue; } for(int j=K;j--;)if(d[u[j][a[i]]]>d[l])a[i]=u[j][a[i]],c[i]+=1<<j; for(int j=K;j--;)if(d[u[j][b[i]]]>d[l])b[i]=u[j][b[i]],c[i]+=1<<j; if(d[u[0][b[i]]]>d[l]||d[u[0][a[i]]]>d[l])c[i]=-1; else ++c[i],v[a[i]].push_back(i); } //判断最上一层 solve(1); rep(i,1,m){ if(c[i]+1-bool(s[i])==0)puts("-1"); else printf("%d\n",c[i]+1-bool(s[i])); } }