转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

E. A and B and Lecture Rooms

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.

Sample test(s)
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
Note

in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.

题意:

给出一棵树,有m次查询,每次查询给出两个点,要求求出这棵树上所有到这两个点距离相等的点的数目

分析:

首先求出两个点的距离,若距离为奇数,则不存在满足要求的点,若距离为偶数,则去路径上的中点,求出所有以从中点连出的分支,同时减去与给出的两点在同一分支上的点的数目

考虑到后面要求深度较深的点向上爬距离为两点间距离一半路程,用倍增在前面处理过的话,这里可以用log n 得到,所以采取了倍增

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
vector<int>G[MAXN];
int root;
int size[MAXN];
int tot;
int pa[][MAXN];
int depth[MAXN];
void dfs(int v,int fa,int d){
pa[][v]=fa;
size[v]=;
depth[v]=d;
for(int i=;i<G[v].size();i++){
if(G[v][i]==fa)continue;
dfs(G[v][i],v,d+);
size[v]+=size[G[v][i]];
}
}
void init(){
dfs(,-,);
for(int k=;k+<;k++){
for(int v=;v<tot;v++){
if(pa[k][v]<)pa[k+][v]=-;
else pa[k+][v]=pa[k][pa[k][v]];
}
}
}
int lca(int u,int v){
if(depth[u]>depth[v])swap(u,v);
int dis=depth[v]-depth[u];
for(int k=;k<;k++){
if((dis>>k)&){
v=pa[k][v];
}
}
if(u==v)return u;
for(int k=;k>=;k--){
if(pa[k][u]!=pa[k][v]){
u=pa[k][u];
v=pa[k][v];
}
}
return pa[][u];
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
tot=n;
int u,v;
for(int i=;i<n-;i++){
cin>>u>>v;
u--;v--;
G[u].push_back(v);
G[v].push_back(u);
}
init();
int m;
cin>>m;
for(int i=;i<m;i++){
cin>>u>>v;
u--;v--;
int r=lca(u,v);
int dis=depth[u]+depth[v]-*depth[r];
if(depth[u]>depth[v])swap(u,v);
if(dis&){
cout<<<<endl;
}else{
dis/=;
int mid=v;
for(int k=;k>=;k--){
if((dis>>k)&){
mid=pa[k][mid];
}
}
int ans=;
if(mid==r){
int preu=u,prev=v;
int du=depth[u]-depth[r];
du--;
for(int k=;k>=;k--){
if((du>>k)&){
preu=pa[k][preu];
}
}
int dv=depth[v]-depth[r];
dv--;
for(int k=;k>=;k--){
if((dv>>k)&){
prev=pa[k][prev];
}
}
ans=tot-size[preu]-size[prev];
}else{
int prev=v,preu=u;
int dv=depth[v]-depth[mid];
dv--;
for(int k=;k>=;k--){
if((dv>>k)&){
prev=pa[k][prev];
}
}
ans=size[mid]-size[prev];
}
cout<<ans<<endl;
}
} return ;
}

codeforces 519E 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

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

  3. CodeForces 519E A and B and Lecture Rooms(倍增)

    A and B are preparing themselves for programming contests. The University where A and B study is a s ...

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

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

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

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

  6. A and B and Lecture Rooms(LCA)

    题目描述 A and B are preparing themselves for programming contests. The University where A and B study i ...

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

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

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

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

随机推荐

  1. 编译Boost 详细步骤

    vs2008编译boost [一.Boost库的介绍] Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会 ...

  2. [POJ] 3461 Oulipo [KMP算法]

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23667   Accepted: 9492 Descripti ...

  3. [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product

    Problem A. Minimum Scalar Product   This contest is open for practice. You can try every problem as ...

  4. SIEM

    http://en.wikipedia.org/wiki/Security_information_and_event_management http://en.wikipedia.org/wiki/ ...

  5. 在Struts2中集成Spring详细讲解

    Spring的官方定义是:一个轻量级的IoC和Aop容器框架,它使用了一种叫做依赖注入的技术. 所谓依赖注入,就是指将创建对象以及协议依赖对象之间合作的责任从对象自身中转移到"工厂" ...

  6. RFC 2327--SDP

    Network Working Group M. Handley Request for Comments: 2327 V. Jacobson Category: Standards Track IS ...

  7. linux中的文件结构

    linux下的文件结构,看看每个文件夹都是干吗用的 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的 ...

  8. 用特殊字体来实现矢量ICON

    用特殊字体来实现矢量ICON tips:其实每个icon都是一个unicode字符,所以,可以通过改变font-size实现icon的矢量放大:可以通过改变color实现多色系.

  9. UVA 10285 Longest Run on a Snowboard(记忆化搜索)

    Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...

  10. ListView之BaseAdapter

    BaseAdapter可以实现自定义的丰富子项视图,本文实现如下所示结果: 实现代码: /* ListView :列表 BaseAdapter 通用的基础适配器 * * */ public class ...