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 ...
随机推荐
- POJ 1002 - 487-3279 STL
先把不是标准格式的字符串变成标准格式再输出出现两次以上的标准串和出现的次数不然输出 "No duplicates." #include <iostream> #incl ...
- hdu 2438
Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...
- PHP 读取EXCEL
PHPExcel 自己下载. PHP读取EXCEL public function import_Excel($file_name){ include_once LIB_ROOT_PATH." ...
- 最常用的CSS技巧收集笔记
1.重置浏览器的字体大小 重置浏览器的默认值 ,然后重设浏览器的字体大小你可以使用雅虎的用户界面重置的CSS方案 ,如果你不想下载9MB的文件,代码如下: body,div,dl,dt,dd,ul, ...
- sublime text 发现一个超好的编辑器
垂直竖行多行编辑 鼠标中建拖动或 shift+右键拖动 切换文件 ctrl+p 输入文件名 可以拖动项目文件夹到sublime text左栏, 也可文件--打开文件夹--项目所在文件夹,但会在新窗口中 ...
- Server2008R2:由于没有远程桌面授权服务器可以提供许可证,.....错误的解决 ---设计师零张
一直使用远程桌面连接一台windows2008server服务器,今天突然报错,连不上了: “由于没有远程桌面授权服务器可以提供许可证,远程会话被中断.请跟服务器管理员联系.” 由于是 ...
- 鼠标悬停移除更换class
$("#xinl").mouseover(function() //鼠标悬停执行函数 { $(".xl").removeClass().addClass(&q ...
- kafka文档翻译(一)
原文来自(http://kafka.apache.org/documentation.html) 本文只做简单的翻译,水平有限,仅供学习交流使用 如有错误,欢迎点评指正 1 准备开始 1.1 介绍 ...
- Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- CloudStack cloud数据库op_host_capacity表type与控制板上的内容的对应关系
listCapacity: type 名称 0 内存 1 CPU 3 主存储 4 公用IP地址 5 管理类IP地址 6 辅助存储 7 VLAN 9 本地存储 ViewResponseHelper.ja ...