题目描述

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. 20165230田坤烨网络对抗免考报告_基于WIN10的渗透攻击

    目录 简单信息收集 主机发现 ping nmap 端口扫描 nmap OS及服务版本探测 nmap -sV 绕过防火墙尝试 诱饵 随机数据长度 随机顺序扫描目标 MAC地址欺骗 实现win10的渗透攻 ...

  2. Shell脚本之二 变量、字符串和数组

    一.Shell 变量 1.1 定义变量 定义变量时,变量名不加美元符号($),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的 ...

  3. Python【每日一问】21

    问: [基础题]输入某年某月某日,判断这一天是这一年的第几天? [提高题]用 *号输出字母 C的图案 答: [基础题]输入某年某月某日,判断这一天是这一年的第几天? 方法1: import time ...

  4. C的位运算符

    1.前言 C的位运算符有&(按位与).|(按位或).^(按位异或).~(按位取反),位运算符把运算对象看作是由二进制位组成的位串信息,按位完成指定的运算,得到相应的结果. 2.位运算符 在上面 ...

  5. 在C++中调用FFTW

    FFTW是一个可以进行可变长度一维或多维DFT的开源C程序库,是目前最快的FFT算法实现. 本文简述了在Windows平台上,如何在C++中调用FFTW,所使用的IDE为Visual Studio 2 ...

  6. NodeJS 使用内容以及模拟一个接口

    1.结合上一篇 安装完Nodejs之后 通过手动创建一个完整的NodeJs项目 2.https://www.jianshu.com/p/7b0a5d4491ba 创建一个完整的项目之后 3.下面是一个 ...

  7. 怎么看系统是UEFI还是Legacy BIOS启动模式?

    在命令行 cmd 中输入  msinfo32 ,找到右侧[BIOS模式],看到这里显示的是[uefi],那么说明是[uefi]方式启动的,反之显示为[BIOS],那么就是传统[BIOS]启动模式.如下 ...

  8. .NET CORE 中的缓存使用

    Net Framewoke的缓存 1.1 System.Web.Caching System.Web.Caching应该是我们最熟悉的缓存类库了,做ASP.NET开发时用到缓存基本都是使用的这个缓存组 ...

  9. 300iq Contest 1 简要题解

    300iq Contest 1 简要题解 咕咕咕 codeforces A. Angle Beats description 有一张\(n\times m\)的方阵,每个位置上标有*,+,.中的一种. ...

  10. Windows和Linux简单命令的总结

    MS-DOS 命令提示符(cmd) 启动:                      Win+R,输入cmd回车 切换盘符            盘符名称: 进入文件夹              cd ...