其他分享
首页 > 其他分享> > codeforces.ml/contest/519/problem/E 求树上到任意两个点距离相等的点 树上倍增 分类讨论

codeforces.ml/contest/519/problem/E 求树上到任意两个点距离相等的点 树上倍增 分类讨论

作者:互联网

E. A and B and Lecture Rooms time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard 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 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
Note

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

 

 

题意:

给两个点 a,b ,问树上和它们距离一样的点有多少个

 

分析:

有三种情况:

1. 如果两个点的深度相等。则除了父节点是 lca 的子树的点到两个点的距离都是相等的,除了这两条边,其它点都是。

 

 2.如果两个点距离是奇数,树上没有和两个点距离相等的点

 

 3.如果两个点的距离是偶数,则中点的除了 包含a或者 b 的所有子树上的点到两个点的距离相等

 

 

//#define int ll
const int N = 1e5+10;
int n,m;
int e[N],ne[N],w[N],h[N],idx,tot,dep[N],p[N][26],sz[N];

void add(int a ,int b) {
    e[idx] = b,ne[idx] = h[a],h[a] = idx ++ ;
}

void dfs(int u,int fa,int d) {
    dep[u] = d;p[u][0] = fa;sz[u] = 1;
    fe(i,u) {
        int j = e[i];
        if(j != fa) {
            dfs(j,u,d+1);
            sz[u] += sz[j];
        }
    }
}

void pre() {
    for(int j = 1;(1<<j) <= n;j++) {
        for(int i = 1;i<=n;i++) {
            if(p[i][j-1] != -1) 
                p[i][j] = p[p[i][j-1]][j-1];
        }
    } 
}

int calc(int u,int d) {
    for(int j = 25;j >= 0; j -- ) {
        if(dep[u] - (1<<j) >= d) u = p[u][j];
    }
    return u;
}

int lca(int x,int y) {
    for(int j = 25;j>=0;j--) {
        if(dep[x] - (1<<j) >= dep[y]) x = p[x][j];
    }
    if(x == y) return x;
    for(int j = 25;j>=0;j -- ) {
        if(p[x][j] != -1 && p[x][j] != p[y][j]) {
            x = p[x][j];
            y = p[y][j];
        }
    }
    return p[x][0];
}

void solve()
{
    ms(h,-1);ms(p,-1);idx = 0;
//    cin>>n>>m;
    int u,v;
    fo(i,1,n-1) {
        cin>>u>>v;
        add(u,v);add(v,u);
    }
    dfs(1,-1,0);
    pre();
    cin>>m;
    int a,b;
    while(m -- ) {
        cin>>a>>b;
        if(dep[a] < dep[b]) swap(a,b);
        if(a == b) {
            cout<<n<<endl;continue;
        }
        int lcaq = lca(a,b);
        int dist = dep[a] + dep[b] - 2 * dep[lcaq];
        if(dist & 1) {
            cout<<0<<endl;continue;
        }
        if(dep[a] == dep[b]) {
            int xa = calc(a,dep[lcaq] + 1);
            int xb = calc(b,dep[lcaq] + 1);
            cout<<n - sz[xa] - sz[xb]<<endl;
        } else {
            int mid = dist / 2 - dep[b] + 2 * dep[lcaq];
            cout<<sz[calc(a,mid)] - sz[calc(a,mid+1)] <<endl;
        }
    }
}

 

标签:room,contest,int,ml,number,dep,rooms,th,树上
来源: https://www.cnblogs.com/er007/p/16659024.html