CodeForces 519E 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 - 1corridors 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.

Examples
input
Copy
4
1 2
1 3
2 4
1
2 3
output
Copy
1
input
Copy
4
1 2
2 3
2 4
2
1 2
1 3
output
Copy
0
2

题意:给出一棵树,m组询问,询问与点u和点v距离相等的点的个数

题解:首先最容易胡出来的是如果一个点到u和v的距离相等,他们肯定是u到v路径上中点的非u、v链以外的全部子树大小之和
然后考虑倍增计算u到v的距离,显然中心会在深度较大的那个点到u与vlca的路径上,高度为距离除二
很明显,如果距离为奇数就无解,为偶数则可以从较低的点直接倍增跳上去,得到这个中点的子树中含点u的那个,直接减去
至于含点v的肯定是父节点那条,不予考虑
因为上面的前提是两点深度不等,所以深度相等的时候要特判,显然u和v都要倍增往上跳,不存在一条路径来自其父亲
然后两点相等最好也特判一下,大概就能A了

代码如下:
#include<map>
#include<set>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cctype>
#include<string>
#include<sstream>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m,fa[18][100010],deep[100010],size[100010];
vector<int> g[100010];

void dfs(int now,int f,int dep)
{
    deep[now]=dep;
    fa[0][now]=f;
    size[now]=1;
    for(int i=1;i<=17;i++)
    {
        fa[i][now]=fa[i-1][fa[i-1][now]];
    }
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==f) continue;
        dfs(g[now][i],now,dep+1);
        size[now]+=size[g[now][i]];
    }
}

int get(int u,int v)
{
    if(u==v) return n;
    if(deep[u]==deep[v])
    {
        for(int i=17;i>=0;i--)
        {
            if(fa[i][u]!=fa[i][v])
            {
                u=fa[i][u];
                v=fa[i][v];
            }
        }
        return n-size[u]-size[v];
    }
    if(deep[u]<deep[v]) swap(u,v);
    int x=u,y=v,dis1=0,dis2=0;
    for(int i=17;i>=0;i--)
    {
        if(deep[fa[i][x]]>=deep[y])
        {
            x=fa[i][x];
            dis1+=pow(2,i);
        }
    }
    if(x==y)
    {
        if(dis1%2==1) return 0;
        int need=dis1/2;
        need--;
        for(int i=17;i>=0;i--)
        {
            if(need&(1<<i))
            {
                u=fa[i][u];
            }
        }
        return size[fa[0][u]]-size[u];
    }
    else
    {
        for(int i=17;i>=0;i--)
        {
            if(fa[i][x]!=fa[i][y])
            {
                x=fa[i][x];
                dis1+=pow(2,i);
                y=fa[i][y];
                dis2+=pow(2,i);
            }
        }
        if((dis1+dis2+2)%2==1) return 0;
        int need=(dis1+dis2+2)/2;        
        need--;
        for(int i=17;i>=0;i--)
        {
            if(need&(1<<i))
            {
                u=fa[i][u];
            }
        }
//        printf("nowu:%d\n",u);
        return size[fa[0][u]]-size[u];
    }
}

int main()
{
    scanf("%d",&n);
    int from,to;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&from,&to);
        g[from].push_back(to);
        g[to].push_back(from);
    }
    dfs(1,0,1);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&from,&to);
        printf("%d\n",get(from,to));
    }
}




猜你喜欢

转载自www.cnblogs.com/stxy-ferryman/p/9363209.html