E. NN country+tree

E. NN country
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the NN country, there are n

cities, numbered from 1 to n, and n1

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?

Input

The first line contains a single integer n

( 2n2105

) — the number of cities.

The second line contains n1

integers p2,p3,,pn ( 1pi<i), where pi means that cities pi and i

are connected by road.

The third line contains a single integer m

( 1m2105

) — the number of bus routes.

Each of the next m

lines contains 2 integers a and b ( 1a,bn, ab), 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

( 1q2105

) — the number of questions you are interested in.

Each of the next q

lines contains 2 integers v and u ( 1v,un, vu), meaning that you are interested if it is possible to get from city v to city u

and what is the minimum number of buses you need to use for it.

Output

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.

Examples
Input
Copy
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
Output
Copy
1
3
-1
1
2
3
Input
Copy
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
Output
Copy
1
-1
-1
1
-1
1
Note
Routes for first sample are marked on the picture.




#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]));
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值