LCA好(Codeforces Round #294 (Div. 2)E. A and B and Lecture Rooms)

E. A and B and Lecture Rooms

A and B are preparing themselves for programming contests.

The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to n.

Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.

As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following m days.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of rooms in the University.

The next n - 1 lines describe the corridors. The i-th of these lines (1 ≤ i ≤ n - 1) contains two integers ai and bi (1 ≤ ai, bi ≤ n), showing that the i-th corridor connects rooms ai and bi.

The next line contains integer m (1 ≤ m ≤ 105) — the number of queries.

Next m lines describe the queries. The j-th of these lines (1 ≤ j ≤ m) contains two integers xj and yj (1 ≤ xj, yj ≤ n) that means that on the j-th day A will write the contest in the room xj, B will write in the room yj.

Output

In the i-th (1 ≤ i ≤ m) line print the number of rooms that are equidistant from the rooms where A and B write contest on the i-th day.

Sample test(s)
Input
4
1 2
1 3
2 4
1
2 3
Output
1
Input
4
1 2
2 3
2 4
2
1 2
1 3
Output
0
2
Note

in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.


题意:给定一棵树,M次询问,每次询问(u,v)到达这两个点距离相等的点有多少个

思路:求出u,v的公共祖先LCA,然后分情况讨论就可以了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int N,M;
vector<int> edge[maxn];
int dep[maxn],fa[maxn],anc[maxn][30];
int num[maxn];
void dfs(int u,int f,int depth)
{
    dep[u]=depth;
    fa[u]=f;
    num[u]=1;
    int len=edge[u].size();
    for(int i=0;i<len;i++)
    {
        int v=edge[u][i];
        if(v==f)continue;
        dfs(v,u,depth+1);
        num[u]+=num[v];
        fa[v]=u;
    }
}
void preprocess()
{
    for(int i=1;i<=N;i++)
    {
        anc[i][0]=fa[i];
        for(int j=1;(1<<j)<=N;j++)anc[i][j]=-1;
    }
    for(int j=1;(1<<j)<=N;j++)
    {
        for(int i=1;i<=N;i++)
        {
            if(anc[i][j-1]==-1)continue;
            int a=anc[i][j-1];
            anc[i][j]=anc[a][j-1];
        }
    }
}
int LCA(int p,int q)
{
    if(dep[p]<dep[q])swap(p,q);
    int k=0;
    while((1<<(k+1))<=dep[p])k++;
    for(int i=k;i>=0;i--)
        if(dep[p]-(1<<i)>=dep[q])
        p=anc[p][i];
    if(p==q)return p;
    for(int i=k;i>=0;i--)
        if(anc[p][i]!=-1&&anc[p][i]!=anc[q][i])
            p=anc[p][i],q=anc[q][i];
    return fa[p];
}
int jump(int u,int dis)
{
    if(dis==0)return u;
    for(int i=19;i>=0;i--)
        if((1<<i)<=dis)
            u=anc[u][i],dis-=(1<<i);
    return u;
}
int solve(int u,int v)
{
    if(u==v)return N;
    if(dep[u]<dep[v])swap(u,v);
    int k=LCA(u,v);
    int dis=dep[u]+dep[v]-dep[k]*2;
    if(dis&1)return 0;
    dis>>=1;
    int mid=jump(u,dis),ans=0;
    if(k==mid)
    {
        u=jump(u,dis-1);
        v=jump(v,dis-1);
        return N-num[u]-num[v];

    }
    else
    {
        u=jump(u,dis-1);
        return num[mid]-num[u];
    }
}
int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        int u,v;
        for(int i=0;i<=N;i++)edge[i].clear();
        for(int i=1;i<N;i++)
        {
            scanf("%d%d",&u,&v);
            edge[u].push_back(v);
            edge[v].push_back(u);
        }
        dfs(1,-1,0);
        preprocess();
        scanf("%d",&M);
        while(M--)
        {
            scanf("%d%d",&u,&v);
            printf("%d\n",solve(u,v));
        }
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值