cf519E

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.

 

题意:给一颗树,每次询问到树上x、y距离相等的点有多少个。

简单的树dp。

//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e5+10;
int n,q,mi[20];

int aa;char cc;
int read() {
	aa=0;cc=getchar();
	while(cc<'0' ||cc>'9') cc=getchar();
	while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
	return aa;
}

int fir[maxn],nxt[2*maxn],to[2*maxn],e=0;
void add(int x,int y) {
	to[++e]=y;nxt[e]=fir[x];fir[x]=e;
	to[++e]=x;nxt[e]=fir[y];fir[y]=e;
}

int fa[maxn][20],d[maxn],size[maxn];
void dfs(int pos,int dep) {
	d[pos]=dep;size[pos]=1;
	for(int i=1;i<=18;++i) fa[pos][i]=fa[fa[pos][i-1]][i-1];
	for(int y=fir[pos];y;y=nxt[y]) {
		if(to[y]==fa[pos][0]) continue;
		fa[to[y]][0]=pos;
		dfs(to[y],dep+1);
		size[pos]+=size[to[y]];
	}
}

int get_lca(int x,int y) {
	int cha=d[x]-d[y];
	for(int i=18;i>=0&&cha;--i) if(cha>=mi[i]) {
		cha-=mi[i]; x=fa[x][i];
	} 
	if(x==y) return x;
	for(int i=18;i>=0;--i) if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}

int work(int x,int y) {
	if(x==y) return n;
	if(d[x]<d[y]) swap(x,y);
	int lca=get_lca(x,y),l=d[x]+d[y]-2*d[lca];
	if(l&1) return 0; int ll=(l>>=1);
	for(int i=18;i>=0&&(l-1)>0;--i) if(l>mi[i]) l-=mi[i],x=fa[x][i];
	if(fa[x][0]!=lca) return size[fa[x][0]]-size[x];
	else {
		l=ll; for(int i=18;i>=0&&(l-1)>0;--i) if(l>mi[i]) l-=mi[i],y=fa[y][i];
		return n-size[x]-size[y];
	}
}

int main() {
	n=read(); int x,y; mi[0]=1;
	for(int i=1;i<=18;++i) mi[i]=mi[i-1]*2;
	for(int i=1;i<n;++i) {
		x=read();y=read();
		add(x,y);
	}
	dfs(1,1); size[0]=size[1];
	q=read();
	for(int i=1;i<=q;++i) {
		x=read();y=read();
		printf("%d\n",work(x,y));
	}
	return 0;
}

  

posted @ 2017-09-23 10:33  shixinyi  阅读(738)  评论(0编辑  收藏  举报