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. OD 实验(十三) - 对一个程序的逆向

    程序: 运行程序 点击 Start,它就会进行对系统的扫描 点击 About -> Enter Registration Code 随便输入一下内容,点击 OK,会弹出该弹窗 用 PEiD 看一 ...

  2. 转 Quartz将Job持久化所需表的说明

      QRTZ_CALENDARS 以 Blob 类型存储 Quartz 的 Calendar 信息 QRTZ_CRON_TRIGGERS 存储 Cron Trigger,包括 Cron表达式和时区信息 ...

  3. DOM笔录

    文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...

  4. Oracle 创建表空间借鉴 保留,占版权留言告知

    /*分为四步 */ /*第1步:创建临时表空间 */ create temporarytablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i ...

  5. 解决oracle11g无法导出空表问题

    11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segmen ...

  6. Programming Entity Framework-dbContext 学习笔记第五章

    ### Programming Entity Framework-dbContext 学习笔记 第五章 将图表添加到Context中的方式及容易出现的错误 方法 结果 警告 Add Root 图标中的 ...

  7. UINavigation,UiView,ModalView Controller之间的关系

    如果当前是个VC,那么就太简单了,直接就可以push到下一个vc AddShopViewController *controller = [[AddShopViewController alloc] ...

  8. PHP 数组中出现中文乱码,json_encode返回结果为null 或false

    想要解决这个问题,没有特别方便的方法,只有循环数组,将数组中的key和value字符串转码,转换为utf-8,即可解决问题. 代码示例:

  9. lnline Hook初试

    简单解释下hook: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到达后,在目标窗口处理 ...

  10. MFC常用函数总结

    1.MFC编辑框.静态文本框相关的常用函数 <1>GetDlgItemText(ID ,str) 作用:从对话框中获取文本 第一个参数为要获取的编辑框(或者静态文本框.单选按钮等可以显示内 ...