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

原题链接

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

对于给出的a, b两点,求出它们的最近公共祖先,lca, d[i]表示i节点的深度, sum[i]表示以i为根的树的节点个数

1.a和b关于lca对称,则int h =d[lca] - d[a], 那么求出距离a, b为h-1的父节点k1, k2, 答案为n - sum[k1] - sum[k2];

2.a和b关于lca不对称,假设d[b] > d[a], d = d[lca] - d[a] + d[lca] - d[b]; d /= 2;求出距离b为d-1的父节点k1, 和距离为d的父节点k2,  答案为sum[k2] - sum[k1];

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <cmath>
#define maxn 100005
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long ll;

int sum[maxn], p[maxn][30], depth[maxn];
vector<int> v[maxn];
void dfs(int j, int f, int h){
	sum[j] = 1;
	depth[j] = h;
	for(int i = 0; i < v[j].size(); i++){
		int k = v[j][i];
		if(k != f){
			dfs(k, j, h+1);
			sum[j] += sum[k];
			p[k][0] = j;
		}
	}
}
int LCA(int a, int b){
	int d = depth[b] - depth[a];
	for(int i = 0; i < 30; i++)
	 if(d&(1<<i))
	   b = p[b][i];
	if(a == b)
	 return a;
	for(int i = 29; i >= 0; i--){
		if(p[a][i] != p[b][i]){
			a = p[a][i];
			b = p[b][i];
		}
	}
	return p[a][0];
}
int main(){
//	freopen("in.txt", "r", stdin);
	int n, a, b;
	scanf("%d", &n);
	for(int i = 0; i < n - 1; i++){
		scanf("%d%d", &a, &b);
		v[a].push_back(b);
		v[b].push_back(a);
	}
	dfs(1, -1, 0);
	for(int i = 1; (1<<i) <= n; i++)
	 for(int j = 1; j <= n; j++){
	 	p[j][i] = p[p[j][i-1]][i-1];
	 }
	int m;
	scanf("%d", &m);
	while(m--){
		scanf("%d%d", &a, &b);
		if(a == b){
			printf("%d\n", n);
			continue;
		}
		if(depth[a] > depth[b])
	      swap(a, b);
		int lca = LCA(a, b);
		int d1 = depth[a] - depth[lca];
		int d2 = depth[b] - depth[lca];
		if(d1 != d2){
			if((d2 + d1) & 1){
				puts("0");
			    continue;
			}
			int mid = (d1 + d2) / 2;
			int k1 = b, k2 = b;
			for(int i = 0; i < 30; i++){
				if(mid&(1<<i))
				  k1 = p[k1][i];
			}
			mid--;
			for(int i = 0; i < 30; i++){
				if(mid&(1<<i))
				 k2 = p[k2][i];
			}
			printf("%d\n", sum[k1] - sum[k2]);
		}
		else{
			d1--;
			for(int i = 0; i < 30; i++)
			 if(d1&(1<<i))
			  a = p[a][i];
			for(int i = 0; i < 30; i++)
			 if(d1&(1<<i))
			  b = p[b][i];
			printf("%d\n", n - sum[a] - sum[b]); 
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值