您的位置:首页 > 其它

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

2016-10-29 15:08 543 查看
原题链接

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: