CodeForces Round #294 (Div. 2) E

E. A and B and Lecture Rooms

  • 原题

传送门RUA!

Time Limit 2s

Memory Limit 256M

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

Input1

4
1 2
1 3
2 4
1
2 3

Output1

Input2

4
1 2
2 3
2 4
2
1 2
1 3

Output2

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个节点的树&q个询问,每次询问两个节点,要求输出树上到这两个节点距离相等的节点个数。

  • Solution

分两种情况讨论

  1. 当两个点深度一样的时候,整棵树上不是这两个点的子孙的节点都是可以选用的

  2. 当两个点深度不同时,又要分两种情况quq

I.他们的链上没有中间点,直接输出0,(柿子先找软的捏对吧

pic1

II.他们的链上有中间点(这边打个lca),就把这个中间点的子树中除掉较深的那个点的子树即可

pic2

PS:本题的图是我第一次wa掉的数据...

本体坑点超多(一定是我太弱了),代码里面详细解释。

  • Codehere

    #include <cstdio>
    #include <vector>
    #define N 100010
    
    using namespace std;
    
    int n, q, a, b, x, y, xx, yy, l, ans, deep, p, d[N], size[N], c[N][21];
    vector <int> e[N];
    
    void dfs (int x, int fa, int dep) {
    	vector <int> :: iterator it;
    	d[x] = dep;
    	size[x] = 1;//子树大小
    	c[x][0] = fa;
    	for (int i = 1; i <= 20; ++i) {
    		c[x][i] = c[c[x][i - 1]][i - 1];//倍增
    	}
    	for (it = e[x].begin(); it != e[x].end(); ++it) {
    		int son = *it;
    		if (son == fa) continue;
    		dfs (son, x, dep + 1);
    		size[x] += size[son];
    	}
    }
    
    int lca (int &x, int &y) {//lca打得很丑,不要介意qwq
    	if (x == y) return x;
    	int jump, i;
    	if (d[x] < d[y]) {
    		jump = d[y] - d[x];
    		for (i = 20; i >= 0; --i) {
    			if (jump & (1 << i)) {
    				y = c[y][i];
    				jump -= (1 << i);
    			}
    			if (jump == 0) break;
    		}
    		if (x == y) return x;
    	}
    	if (d[x] > d[y]) {
    		jump = d[x] - d[y];
    		for (i = 20; i >= 0; --i) {
    			if (jump & (1 << i)) {
    				x = c[x][i];
    				jump -= (1 << i);
    			}
    			if (jump == 0) break;
    		}
    		if (x == y) return x;
    	}
    	if (d[x] == d[y]) {
    		for (i = 20; i >= 0; --i) {
    			if (c[x][i] != c[y][i]) {
    				x = c[x][i];
    				y = c[y][i];
    			}
    		}
    		return c[x][0];
    	}
    }
    
    int main () {
    	scanf ("%d", &n);
    	for (int i = 1; i < n; ++i) {
    		scanf("%d%d", &a, &b);
    		e[a].push_back(b);
    		e[b].push_back(a);
    	}
    	dfs (1, 1, 1);//建树
    	scanf("%d", &q);
    	for (int i = 1; i <= q; ++i) {
    		scanf("%d%d", &x, &y);
    		if (x == y) {//同一个点的话直接输出n
    			printf("%d\n", n);
    			continue;
    		}
    		if (d[x] == d[y]) {
    			l = lca (x, y);
    			ans = n - size[x] - size[y];
    			printf ("%d\n", ans);
    			continue;
    		}
    		if (d[x] != d[y]) {
    			if ((d[x] + d[y]) % 2 == 1) {
    				printf ("0\n");
    				continue;
    			}
    			if (d[x] < d[y]) p = y;
    			else p = x;
    			xx = x;
    			yy = y;
    			l = lca (xx, yy);
    			int jump = (d[x] + d[y] - d[l] * 2) / 2 - 1;//刚开始忘记减1了,调了很久
    			for (int j = 20; j >= 0; --j) {
    				if (jump & (1 << j)) {
    					p = c[p][j];
    					jump -= (1 << j);
    				}
    				if (jump == 0) break;
    			}
    			ans = size[c[p][0]] - size[p];
    			printf ("%d\n", ans);
    		}
    	}
    	return 0;
    }

 

猜你喜欢

转载自blog.csdn.net/jokingcoder/article/details/81103378