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.
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.
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.
4 1 2 1 3 2 4 1 2 3
1
4 1 2 2 3 2 4 2 1 2 1 3
0 2
in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
一开始题意读错了,坑死了。题意:给定一棵树,m次询问,每次询问给两个点a,b,求这棵树上有多少个到这两点距离相等的点,注意是最短距离相等,不可重复走一条边。首先肯定要LCA啦,然后分情况讨论,比如a==b这时候输出n。
root=LCA(a,b);
然后求一下a到root的距离加上b到root的距离k。如果K是奇数则输出0.
如果K是偶数,则要继续分情况讨论。第一种情况是a和b的深度不等,这时候要求出中间那个点,然后答案就是这个点的 不包含a和b 的子树,就是比如这个点有k个儿子,其中肯定有一个儿子下面有a或者b,总的子树去掉这个儿子包含的子树,就是答案。
第二种情况,a和b的深度相等,那么答案就是root上面的所有点和root的不包含a和b的儿子的子树。
然后先预处理出以每个点为根的树有多少个节点,然后再用ST表来求某个点的第几个祖先是多少来找那个中间点和中间的的特殊儿子节点。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define maxn 100005
using namespace std;
int n,m;
int final_ans;
int flagup[3];
int flagdown;
int tmpp[maxn];
vector<int>p[maxn];
int fat[100005][20],lev[100005],father[maxn];
void dfs(int x,int fa){
father[x]=fa;
fat[x][0]=fa;
lev[x]=lev[fa]+1;
int e=p[x].size();
for(int i=0;i<e;i++){
int y=p[x][i];
if(y==fa) continue;
dfs(y,x);
}
}
int lca(int x,int y){
if(lev[x]>lev[y]) swap(x,y);
for(int i=19;i>=0;i--){
if(lev[fat[y][i]]>=lev[x]){
y=fat[y][i];
}
}//拉到同一高度
for(int i=19;i>=0;i--){
if(fat[x][i]!=fat[y][i]){
x=fat[x][i];
y=fat[y][i];
}
}//一起往上找
return (x==y) ? x:fat[x][0];
}
void intit()
{
int i, j;
dfs(1,0);
for(i=1;i<=19;i++){
for(j=0;j<=n;j++){
fat[j][i]=fat[fat[j][i-1]][i-1];
}
}
}//初始化处理
int find_dfs(int now,int fa)
{
///
int e=p[now].size();
for(int i=0;i<e;i++){
int y=p[now][i];
if(y==fa) continue;
tmpp[now]+=find_dfs(y,now);
}
return tmpp[now];
}
int find_ans(int now,int kk)
{
int j=0;
while(kk)
{
if(kk%2==1)
{
now=fat[now][j];
}
kk>>=1;
j++;
}
return now;
}
int main()
{
int u,v;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<maxn;i++)
{
p[i].clear();
}
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&u,&v);
p[u].push_back(v);
p[v].push_back(u);
}
intit();
scanf("%d",&m);
for(int i=1;i<=n;i++)
tmpp[i]=1;
find_dfs(1,0);
int now;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
int root=lca(u,v);
int k=lev[u]-lev[root]+lev[v]-lev[root];
if(lev[u]<lev[v])
swap(u,v);//u深一点
if(u==v)
{
printf("%d\n",n);
continue;
}
if(k%2)
{
printf("0\n");
continue;
}
if(lev[u]!=lev[v])//需要在k点找,k在LCA的子树中
{
now=find_ans(u,k/2);
k=find_ans(u,k/2-1);
printf("%d\n",tmpp[now]-tmpp[k]);
continue;}
if(lev[u]==lev[v])
{
now=find_ans(u,k/2);
int son1=find_ans(u,k/2-1);
int son2=find_ans(v,k/2-1);
printf("%d\n",tmpp[1]-tmpp[now]+tmpp[now]-tmpp[son1]-tmpp[son2]);
}
}
}
}