E. NN country+tree

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ujn20161222/article/details/80349211

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



The Heart of the Country

12-01

Problem DescriptionnThe nation of Graphia is at war. The neighboring nations have for long watched in jealousy as Graphia erected prosperous cities and connected them with a network of highways. Now they want a piece of the pie.nnGraphia consists of several cities, connected by highways. Graphian terrain is rough, so the only way to move between the cities is along the highways. Each city has a certain number of troops quartered there. Graphia's military command knows that it will require a certain number of troops, K , to defend any city. They can defend a city with the troops stationed there, supported by the troops in any other city which is directly connected with a highway, with no cities in between. Any troops further away than that simply cannot get there in time. They also know that their enemies will onlyattack one city at a time -- so the troops in a city can be used to defend that city, as well as any of its neighbors. However, if a city can't be defended, then the military command must assume that the troops quartered in that city will be captured, and cannot aid in the defense of Graphia. In the case below, suppose K = 10 . City C might seem well defended, but it will eventually fall.n![](http://acm.hdu.edu.cn/data/images/con228-1002-1.JPG)nnnnGraphia's leadership wants to identify the Heart of their country -- the largest possible group of cities that can mutually defend each other, even if all of the other cities fall.nnMore formally, a city is defensible if it can draw a total of at least K troops from itself, and from cities directly adjacent to it. A set of cities is defensible if every city in it is defensible, using only troops from itself and adjacent cities in that set. The Heart of the country is the largest possible defensible set of cities - that is, no other defensible set of cities has more cities in it.n nnInputnThere will be several data sets. Each set begins with two integers, N and K , where N is the number of cities ( 3<=N<=1000 ), and K is the number of troops required to defend a city. The cities are numbered 0 through N - 1 .nnOn the next N lines are descriptions of the cities, starting with city 0. Each of the city description lines begins with an integer T , indicating the number of troops quartered in that city ( 0<=T<=10000 ). This is followed by an integer M , indicating the number of highways going out of that city, and then M integers, indicating the cities those highways go to. No two highways will go from and to the same cities, so every city in each list will be unique. No highway will loop from a city back to the same city. The highways go both ways, so that if city I is in city J 's list, then it's guaranteed that city J will be in city I 's list in the input. The input will end with a line with two space-separated 0's.n nnOutputnFor each data set, print two integers on a single line: The number of cities in the heart of the country, and the number of troops in the heart of the country. Print a space between the integers. There should be no blank lines between outputs.n nnSample Inputn4 900n100 2 1 2n200 2 0 3n500 2 0 3n1000 2 1 2n4 900n100 3 1 2 3n200 3 0 3 2n500 3 1 3 0n1000 3 2 1 0n0 0n nnSample Outputn3 1700n4 1800

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭