您的位置:首页 > 其它

codeforces E. A and B and Lecture Rooms

2015-04-10 12:10 253 查看
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.

Sample test(s)

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.

题意:

给定一棵树,和若干点对,要求这一棵树上到这些点对的距离相等的点的数目。

思路:

经典的lca问题,先找到lca,可以确定下这点对之间的路径。然后找到路径之中距离它们相等的那个点,以这个点为根的节点数就是要求的答案。当然如果这两个点之间的距离是偶数的话,显然不存在,则为0

//
// main.cpp
// A and B and Lecture Rooms
//
// Created by 蘇與軒 on 15/4/10.
// Copyright (c) 2015年 蘇與軒. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#define rep(i,a,b) for (int i=a;i<((b)+1);i++)
#define Rep(i,a,b) for (int i=a;i>=b;i--)
#define foreach(e,x) for (__typeof(x.begin()) e=x.begin();e!=x.end();e++)
#define mid ((l+r)>>1)
#define lson (k<<1)
#define rson (k<<1|1)
#define MEM(a,x) memset(a,x,sizeof a)
using namespace std;
const int N=200050;
const int Mod=1e9+9;
const int inf=0x3f3f3f3f;
typedef pair<int, int> pii;
typedef long long ll;
int pnt
,head
,nxt
,size
,fa
[20],dep
,n,u,v,cnt,m;
void addedge(int u,int v) {
pnt[cnt]=v;nxt[cnt]=head[u];head[u]=cnt++;
}
void dfs(int u) {
size[u]=1;
rep(i,1,19) fa[u][i]=fa[fa[u][i-1]][i-1];
for (int i=head[u];~i;i=nxt[i]) {
int v=pnt[i];
if (v==fa[u][0]) continue;
dep[v]=dep[u]+1;
fa[v][0]=u;
dfs(v);
size[u]+=size[v];
}
}
int go(int len,int x) {
while (len) {
int z=log10(1.0*len)/log10(2.0);
x=fa[x][z];
len-=1<<z;
}
return x;
}
int lca(int x,int y) {
if (dep[x]<dep[y]) swap(x,y);
rep(i,0,19) if ((dep[x]-dep[y])&(1<<i)) x=fa[x][i];
if (x==y) return x;
Rep(i,19,0) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int main(int argc, const char * argv[]) {
scanf("%d",&n);
MEM(head,-1);
rep(i,1,n-1) {
scanf("%d%d",&u,&v);
addedge(u, v);
addedge(v, u);
}
fa[1][0]=1;dep[1]=0;
dfs(1);
scanf("%d",&m);
while (m--) {
scanf("%d%d",&u,&v);
if (u==v) {
printf("%d\n",n);
continue;
}
if (dep[u]>dep[v]) swap(u,v);
int l=lca(u,v);
int len=dep[u]-dep[l]+dep[v]-dep[l];
if (len&1) {
puts("0");
continue;
}
len=len/2-1;
int a=go(len,v),b=fa[a][0];
if (b==l) {
printf("%d\n",n-size[a]-size[go(len,u)]);
continue;
}else printf("%d\n",size[b]-size[a]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: