Codeforces Round #294 (Div. 2) E 树上倍增lca



链接:戳这里


E. A and B and Lecture Rooms
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples
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.


题意:

n个点组成的树,m组询问,每组询问u,v表示,输出有多个点分别到u,v的距离相等


思路:

先求出u,v的最近公共祖先。

如果u,v的深度相同,那么n减去lca连接u,v颗子树size就是答案了

如果u,v的深度不同,那么肯定在深度大的那颗子树上,这里找到这个链上的中点,然后中点的size减去连接深度大的点所在的子树就是答案了。还是就是终点不存在的话输出0


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double lb;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n;
int head[500100],tot=0;
struct edge{
    int v,next;
}e[500100];
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
int deep[500100],fa[500100][21];
int sz[500100];
void DFS(int u,int f,int de){
    fa[u][0]=f;
    deep[u]=de;
    sz[u]=1;
    for(int i=head[u];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v==f) continue;
        DFS(v,u,de+1);
        sz[u]+=sz[v];
    }
}
void build(){
    for(int i=1;i<=20;i++){
        for(int j=1;j<=n;j++){
            if(fa[j][i-1]!=-1) fa[j][i]=fa[fa[j][i-1]][i-1];
        }
    }
}
int LCA(int x,int y){
    if(deep[x]<deep[y]) swap(x,y);
    int len=deep[x]-deep[y];
    for(int i=0;i<=20;i++){
        if(len&(1<<i)) x=fa[x][i];
    }
    for(int i=20;i>=0;i--){
        if(fa[x][i]!=fa[y][i]) {
            x=fa[x][i];
            y=fa[y][i];
        }
    }
    if(x==y) return x;
    return fa[x][0];
}
int main(){
    mst(head,-1);
    mst(fa,-1);
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        Add(u,v);
        Add(v,u);
    }
    DFS(1,0,0);
    build();
    int m;
    scanf("%d",&m);
    while(m--){
        int u,v;
        scanf("%d%d",&u,&v);
        if(deep[u]<deep[v]) swap(u,v);
        int lca=LCA(u,v);
        ///printf("lca=%d\n",lca);
        int x=deep[u]-deep[v];
        if(deep[u]==deep[v]) {
            int len=deep[u]-deep[lca]-1;
            int midson_u=u,midson_v=v;
            for(int i=20;i>=0;i--){
                if(len&(1<<i)) {
                    midson_u=fa[midson_u][i];
                    midson_v=fa[midson_v][i];
                }
            }
            ///printf("%d %d\n",midson_u,midson_v);
            printf("%d\n",n-sz[midson_u]-sz[midson_v]);
        }
        else if(x%2==1) printf("0\n");
        else {
            int len=deep[v]-deep[lca]+x/2;
            int lenson=len-1;
            int mid=u,midson=u;
            for(int i=20;i>=0;i--){
                if(len&(1<<i)) {
                    mid=fa[mid][i];
                }
                if(lenson&(1<<i)) midson=fa[midson][i];
            }
            ///printf("%d %d %d %d %d\n",u,len,lenson,mid,midson);
            printf("%d\n",sz[mid]-sz[midson]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值