2 seconds
256 megabytes
standard input
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.
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次询问,每次询问给你两个点u,v,问有多少点到u和v的距离相等。
【分析】先找lca算距离,若距离是奇数,则输出0;若是偶数,两种情况,一,lca就是中点,二,不是...很简单,直接上代码...
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long LL;
const int N = 1e5+;
const int mod = 1e9+;
int n,m;
int dep[N],fa[N][],sz[N];
vector<int>edg[N];
void dfs(int u,int f){
fa[u][]=f;
sz[u]=;
for(int i=;i<;i++){
fa[u][i]=fa[fa[u][i-]][i-];
}
for(int v : edg[u]){
if(v==f)continue;
dep[v]=dep[u]+;
dfs(v,u);
sz[u]+=sz[v];
}
}
int LCA(int u,int v){
int U=u,V=v;
if(dep[u]<dep[v])swap(u,v);
for(int i=;i>=;i--){
if(dep[fa[u][i]]>=dep[v]){
u=fa[u][i];
}
}
if(u==v)return (u);
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
return (fa[u][]);
}
pii findMid(int u,int v,int lca){
if(dep[u]-dep[lca]>dep[v]-dep[lca])swap(u,v);
int vv=v,mid;
for(int i=;i>=;i--){
mid=fa[v][i];
int s1=dep[u]+dep[mid]-*dep[lca];
int s2=dep[vv]-dep[mid];
if(dep[mid]<dep[lca]||s1<=s2)continue;
else v=fa[v][i];
}
return mp(v,fa[v][]);
}
int main(){
int u,v;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
edg[u].pb(v);
edg[v].pb(u);
}
dfs(,);
scanf("%d",&m);
while(m--){
scanf("%d%d",&u,&v);
int lca=LCA(u,v);
int s=dep[u]+dep[v]-*dep[lca];
if(s&)puts("");
else if(u==v)printf("%d\n",n);
else {
if(dep[u]-dep[lca]==dep[v]-dep[lca]){
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
printf("%d\n",n-sz[u]-sz[v]);
}
else {
pii p=findMid(u,v,lca);
int mid=p.second;
int midson=p.first;
printf("%d\n",sz[mid]-sz[midson]);
}
}
}
return ;
}