codeforces 519E A and B and Lecture Rooms(LCA,倍增)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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.
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.
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.
4
1 2
1 3
2 4
1
2 3
1
4
1 2
2 3
2 4
2
1 2
1 3
0
2
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,倍增)的更多相关文章
- codeforces 519E A and B and Lecture Rooms LCA倍增
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
- Codeforces 519E A and B and Lecture Rooms
http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...
- 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 ...
- Codeforces 519E A and B and Lecture Rooms [倍增法LCA]
题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分 ...
- [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)
题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...
- A and B and Lecture Rooms(LCA)
题目描述 A and B are preparing themselves for programming contests. The University where A and B study i ...
- 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 ...
- [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= ...
- CodeForces 519E 树形DP A and B and Lecture Rooms
给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...
随机推荐
- KMP算法———模板
做出KMP字符串匹配算法心情也是好好哒,萌萌哒. 感谢黄学长,感谢栋栋! #include<cstdio>#include<string>#include<iostrea ...
- C++ 虚函数详解
C++ 虚函数详解 这篇文章主要是转载的http://blog.csdn.net/haoel/article/details/1948051这篇文章,其中又加入了自己的理解和难点以及疑问的解决过程,对 ...
- 函数:灵活即强大 - 零基础入门学习Python018
函数:灵活即强大 让编程改变世界 Change the world by program 上节课我们基本介绍了Python函数的用法,非常简单.这节课我们主要针对函数的参数和返回值进一步深入学习.学习 ...
- javax.net.ssl.SSLException: java.lang.UnsupportedOperationException
Loading KeyStore C:\Tool\jdk1.7.0_71\jre\lib\security\jssecacerts... Opening connection to www.googl ...
- 升级10.11后使用CocoaPod出现-bash: pod: command not found 解决办法-备
升级10.11后,运行pod命令出现: -bash: pod: command not found 解决办法: sudo gem install -n /usr/local/bin cocoapods ...
- shell 脚本监控程序是否正在执行, 如果没有执行, 则自动启动该进程
代码里面监控1个进程, 代码很简单, 我就不讲解了, 有不懂的, 可以在回复里面问. 我看见了会给予讲解. 当然了, 该脚本要执行,你需要开启系统的定时器进程 crond , 并且编辑配置文件. 执行 ...
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端
原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端 chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车 ...
- SQL Server 性能优化之——重复索引
原文 http://www.cnblogs.com/BoyceYang/archive/2013/06/16/3139006.html 阅读导航 1. 概述 2. 什么是重复索引 3. 查找重复索引 ...
- 关于google的C++ coding style
大家都知道google的开源项目有很多,不过我观察过一些开源项目,觉得代码质量就是这家最好了.这些“教条”式规定的背后是是来自于常年工程经验积累上的理性思考. 为什么好?主要有以下几点: 1.规范,就 ...
- 实用的VIM配置文件
VIM配置文件名为.vimrc,默认在用户根目录下,或者在命令模式下输入:version可以获取配置文件路径. 在VIM命令行下输入options,然后回车,可以查看VIM所有的参数选项. 双引号&q ...