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.

Examples
input

Copy
4
1 2
1 3
2 4
1
2 3
output

Copy
1
input

Copy
4
1 2
2 3
2 4
2
1 2
1 3
output

Copy
0
2 题意:给出一棵树,m组询问,询问与点u和点v距离相等的点的个数 题解:首先最容易胡出来的是如果一个点到u和v的距离相等,他们肯定是u到v路径上中点的非u、v链以外的全部子树大小之和
然后考虑倍增计算u到v的距离,显然中心会在深度较大的那个点到u与vlca的路径上,高度为距离除二
很明显,如果距离为奇数就无解,为偶数则可以从较低的点直接倍增跳上去,得到这个中点的子树中含点u的那个,直接减去
至于含点v的肯定是父节点那条,不予考虑
因为上面的前提是两点深度不等,所以深度相等的时候要特判,显然u和v都要倍增往上跳,不存在一条路径来自其父亲
然后两点相等最好也特判一下,大概就能A了 代码如下:
#include<map>
#include<set>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cctype>
#include<string>
#include<sstream>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n,m,fa[][],deep[],size[];
vector<int> g[]; void dfs(int now,int f,int dep)
{
deep[now]=dep;
fa[][now]=f;
size[now]=;
for(int i=;i<=;i++)
{
fa[i][now]=fa[i-][fa[i-][now]];
}
for(int i=;i<g[now].size();i++)
{
if(g[now][i]==f) continue;
dfs(g[now][i],now,dep+);
size[now]+=size[g[now][i]];
}
} int get(int u,int v)
{
if(u==v) return n;
if(deep[u]==deep[v])
{
for(int i=;i>=;i--)
{
if(fa[i][u]!=fa[i][v])
{
u=fa[i][u];
v=fa[i][v];
}
}
return n-size[u]-size[v];
}
if(deep[u]<deep[v]) swap(u,v);
int x=u,y=v,dis1=,dis2=;
for(int i=;i>=;i--)
{
if(deep[fa[i][x]]>=deep[y])
{
x=fa[i][x];
dis1+=pow(,i);
}
}
if(x==y)
{
if(dis1%==) return ;
int need=dis1/;
need--;
for(int i=;i>=;i--)
{
if(need&(<<i))
{
u=fa[i][u];
}
}
return size[fa[][u]]-size[u];
}
else
{
for(int i=;i>=;i--)
{
if(fa[i][x]!=fa[i][y])
{
x=fa[i][x];
dis1+=pow(,i);
y=fa[i][y];
dis2+=pow(,i);
}
}
if((dis1+dis2+)%==) return ;
int need=(dis1+dis2+)/;
need--;
for(int i=;i>=;i--)
{
if(need&(<<i))
{
u=fa[i][u];
}
}
// printf("nowu:%d\n",u);
return size[fa[][u]]-size[u];
}
} int main()
{
scanf("%d",&n);
int from,to;
for(int i=;i<=n-;i++)
{
scanf("%d%d",&from,&to);
g[from].push_back(to);
g[to].push_back(from);
}
dfs(,,);
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&from,&to);
printf("%d\n",get(from,to));
}
}


CodeForces 519E A and B and Lecture Rooms(倍增)的更多相关文章

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

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

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

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

  3. 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 ...

  4. Codeforces 519E A and B and Lecture Rooms

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

  5. 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 ...

  6. [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= ...

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

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

  8. CodeForces 519E 树形DP A and B and Lecture Rooms

    给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...

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

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

随机推荐

  1. 硬盘IOPS与读写速度

    IOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一.IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的I/O请求 ...

  2. 【合】C#线程

    浅谈ThreadPool 线程池 地址:https://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html 相关概念: 线程池可以看做容纳线程 ...

  3. MongoDB + Spark: 完整的大数据解决方案

    Spark介绍 按照官方的定义,Spark 是一个通用,快速,适用于大规模数据的处理引擎. 通用性:我们可以使用Spark SQL来执行常规分析, Spark Streaming 来流数据处理, 以及 ...

  4. makefile .phony targets

    Phony Targets PHONY 目标并非实际的文件名:只是在显式请求时执行命令的名字.有两种理由需要使用PHONY 目标:避免和同名文件冲突,改善性能. 如果编写一个规则,并不产生目标文件,则 ...

  5. dclcommon200.bpl

    xe6 dclcommon200.bpl xe7 dclcommon210.bpl xe8  dclcommon220.bpl xe7,xe8都有对应的文件,xe6为何没有?

  6. promtheus 配置文件

    全局配置 global 属于全局的默认配置,它主要包含 4 个属性, scrape_interval: 拉取 targets 的默认时间间隔. scrape_timeout: 拉取一个 target ...

  7. Innobackupex MySQL 全备、增备及恢复

    简介: 在这之前都是通过 mysqldump 来备份数据库的,由于是逻辑备份,所以采用这种备份方式数据是很安全的,跨平台.版本都很容易. 凡事有利必有弊,逻辑备份在你数据库比较大时,备份.恢复数据所耗 ...

  8. XX公司的CA,与平台融合解决方法。。。。。

    1,jsp 的编写要求 <script language="javascript" type="text/javascript"> var clie ...

  9. pid文件的作用

    pid文件的作用 一.pid文件的作用 1.pid文件的内容用cat命令查看,可以看到内容只有一行,记录了该进程的ID 2.pid文件的作用防止启动多个进程副本 3.pid文件的原理进程运行后会给.p ...

  10. Struts2分模块开发

    -------------------siwuxie095 Struts2 分模块开发 在实际开发中,如果一个项目是团队开发的,也就是很多人开发的, 每个人都需要去修改 struts.xml,因为 s ...