题目描述

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. activiti学习7:spring和activiti进行整合

    目录 activiti学习7:spring和activiti进行整合 一.整合原理 二.整合步骤 2.1 新建一个maven工程并导入相关依赖 2.2 创建spring配置文件 三.测试 activi ...

  2. 阿里云配置DDoS高防

  3. Java开发笔记(一百四十五)FXML布局的伸展适配

    前面介绍了FXML的基本格式及其控制器的用法,算是打通了FXML方式的编码流程.程序界面通常保持固定尺寸,不过有时也允许用户拖曳窗口大小,不拖不打紧,一拖就可能坏事.像之前的登录窗口,没拖的时候界面如 ...

  4. jspatch功能解析

    一.三个模型: 1.补丁:运行时结构维护模型: 2.通信模型 3.解释模型:运行时 二.js.oc分层解释调用 js模块与oc模块的关系 1.oc调用js的配置信息完成配置: 2.oc运行时重定位到j ...

  5. 一起来学习.net core程序使用中介者模式:MediatR插件

    中介者模式是一种常见的设计模式,旨再降低程序的耦合性,因为传统的三层模式层层之间需要显示的调用,必须上层依赖下层,耦合性很高,为了解耦,将所有的指令单独放在一个位置处理,其他位置均通过这个位置来间接的 ...

  6. Java静态变量初始化的坑

    class SingleTon { private static SingleTon singleTon = new SingleTon(); public static int count1; pu ...

  7. Vue3.0结合bootstrap做多页面应用(2)基础配置

    接下来就是多页面应用的配置啦,安装完vue 3.0可以发现目录比2.0的精简了很多, public相当于原来的static,里面的index.html是项目的入口 ,src同以前一样,cli3.0没有 ...

  8. 基于tensorflow的简单鼠标键盘识别

    import cv2 as cvimport tensorflow as tfimport numpy as npimport random ##以下为数据预处理,分类为cata,总共样本为cata* ...

  9. springMVC执行流程及架构

    目录 springMVC简单执行流程 springMVC框架 注解实现 springMVC简单执行流程 springMVC框架 执行流程: 1.用户发送请求至前端控制器DispatcherServle ...

  10. Java 数据类型 & 变量与常量 & 注释

    一.数据类型 1.数据类型分类 Java 的数据类型分为两大类: 基本数据类型:整数.浮点数.字符型.布尔型 引用数据类型(对象类型):类.数组,字符串.接口等. 2.基本数据类型 四类八种基本数据类 ...