题目描述

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
解析:
给出树上两点u,v,找到与u,v距离相等的点的个数
先找到u,v的公共祖先r,然后知道u,v间的距离,中间位置为mid;
当然若dis(u,v)为奇数则无解;
将mid的位置进行讨论:
mid在r上:
mid不在r上:
//毒瘤题
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int>G[];
int n,m;
int size[],grand[][],deep[];
//数据范围、数组范围要注意
void build(int x,int pre)
{
for(int i=;i<=;i++)
{
grand[x][i]=grand[grand[x][i-]][i-];
}
size[x]=;
for(int i=;i<G[x].size();i++)
{
int v=G[x][i];
if(v==pre)continue;
deep[v]=deep[x]+;
grand[v][]=x;
build(v,x);
size[x]+=size[v];
}
}
int lca(int u,int v)
{
if(deep[u]<deep[v])swap(u,v);
int dis=deep[u]-deep[v];
///////////////////////////////////
for(int k=;k<;k++){
if((dis>>k)&){
u=grand[u][k];
}
}
//把u调到和v同一深度
///////////////////////////////////
if(u==v)return u;//当且只当u、v处于一条链时
for(int k=;k>=;k--){
if(grand[u][k]!=grand[v][k]){
u=grand[u][k];
v=grand[v][k];
}
}
return grand[u][];
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);//vector可以...节约代码量
}
build(,);//建树
scanf("%d",&m);//m个询问
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
int r=lca(u,v);
int dis=deep[u]+deep[v]-*deep[r];
if(dis&){printf("0\n");}
if(deep[u]>deep[v])swap(u,v);
else
{
dis/=;
/////////////////////////////////////////////////
int mid=v;
for(int k=;k>=;k--){
if((dis>>k)&) mid=grand[mid][k];
}
/////////////////////////找到 mid
int ans=;
if(mid==r)
{
int preu=u,prev=v;
int du=deep[u]-deep[r];du--;
int dv=deep[v]-deep[r];dv--;
for(int k=;k>=;k--){
if((du>>k)&) preu=grand[preu][k];
if((dv>>k)&) prev=grand[prev][k];
}
ans=n-size[preu]-size[prev];
}
else
{
int prev=v,preu=u;
int dv=deep[v]-deep[mid];
dv--;
for(int k=;k>=;k--){
if((dv>>k)&) prev=grand[prev][k];
}
ans=size[mid]-size[prev];
}
cout<<ans<<endl;
}
}
}

A and B and Lecture Rooms(LCA)的更多相关文章

  1. codeforces 519E A and B and Lecture Rooms LCA倍增

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  2. codeforces 519E A and B and Lecture Rooms(LCA,倍增)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud E. A and B and Lecture Rooms A and B are ...

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

    A and B and Lecture Rooms time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. [CF Round #294 div2] E. A and B and Lecture Rooms 【树上倍增】

    题目链接:E. A and B and Lecture Rooms 题目大意 给定一颗节点数10^5的树,有10^5个询问,每次询问树上到xi, yi这两个点距离相等的点有多少个. 题目分析 若 x= ...

  5. Codeforces 519E A and B and Lecture Rooms [倍增法LCA]

    题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分 ...

  6. [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)

    题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...

  7. Codeforces 519 E. A and B and Lecture Rooms

    Description 询问一个树上与两点距离相等的点的个数. Sol 倍增求LCA. 一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点. 有些结论很容易证明,如果距离是偶数,那 ...

  8. Codeforces 519E A and B and Lecture Rooms

    http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...

  9. Codeforces519 E. A and B and Lecture Rooms

    传送门:>Here< 题意:询问给出一棵无根树上任意两点$a,b$,求关于所有点$i$,$dist(a,i) = dist(b,i)$的点的数量.要求每一次询问在$O(log n)$的时间 ...

随机推荐

  1. keepass口令管理实践

    语言修改 加入插件 插件的学习及应用

  2. c# 3d图像显示

    1.应用directx图形库进行开发: 2.代码: public class TClass : System.Windows.Forms.Form { /// <summary> /// ...

  3. 正方教务系统客户端 error loading midas.dll.

    在windows xp/7/10上安装了客户端,安装到注册字体一步,没有响应,强行结束.启动客户端,登录,出现  error loading midas.dll. 32位:先将 midas.dll 放 ...

  4. 【2019年05月07日】A股最便宜的股票

    新钢股份(SH600782) - 当前便宜指数:193.2 - 滚动扣非市盈率PE:2.99 - 滚动市净率PB:0.87 - 动态年化股息收益率:1.68%- 新钢股份(SH600782)的历史市盈 ...

  5. spark 通过keytab 获取认证

    /usr/local/spark--bin--cdh5.8.0/bin/spark-submit \ --keytab /home/jj/tl.keytab \ --principal vf@FC.C ...

  6. centos删除指定名称的进程

    vim kill_process.sh ----------------------------------- if [ "$1" = "" ]; then e ...

  7. VUE后缀页面调试

    在VUE中Js代码可以直接设置断点进行调试,但是vue文件中点击断点无反应,可以在想要断点的地方增加一行代码即可   debugger

  8. FusionInsight大数据开发---MapReduce与YARN应用开发

    MapReduce MapReduce的基本定义及过程 搭建开发环境 代码实例及运行程序 MapReduce开发接口介绍 1. MapReduce的基本定义及过程 MapReduce是面向大数据并行处 ...

  9. laravel5.4 orm with 用法

    在laravel orm 中一个with 关联方法,需要在模板中先定义表与表之间的关系 /*一对多的关系 */ public function hasManyTemplate(){ return $t ...

  10. bytearray与矩阵转换对应关系

    import numpy as npimport osa=bytearray(os.urandom(27))# for i in range(21):# print(a[i])a=np.array(a ...