Codeforces Round #294 (Div. 2) A and B and Lecture Rooms(LCA 倍增)
2 seconds
256 megabytes
standard input
standard output
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次询问,每次询问给你两个点u,v,问有多少点到u和v的距离相等。
【分析】先找lca算距离,若距离是奇数,则输出0;若是偶数,两种情况,一,lca就是中点,二,不是...很简单,直接上代码...
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long LL;
const int N = 1e5+;
const int mod = 1e9+;
int n,m;
int dep[N],fa[N][],sz[N];
vector<int>edg[N];
void dfs(int u,int f){
fa[u][]=f;
sz[u]=;
for(int i=;i<;i++){
fa[u][i]=fa[fa[u][i-]][i-];
}
for(int v : edg[u]){
if(v==f)continue;
dep[v]=dep[u]+;
dfs(v,u);
sz[u]+=sz[v];
}
}
int LCA(int u,int v){
int U=u,V=v;
if(dep[u]<dep[v])swap(u,v);
for(int i=;i>=;i--){
if(dep[fa[u][i]]>=dep[v]){
u=fa[u][i];
}
}
if(u==v)return (u);
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
return (fa[u][]);
}
pii findMid(int u,int v,int lca){
if(dep[u]-dep[lca]>dep[v]-dep[lca])swap(u,v);
int vv=v,mid;
for(int i=;i>=;i--){
mid=fa[v][i];
int s1=dep[u]+dep[mid]-*dep[lca];
int s2=dep[vv]-dep[mid];
if(dep[mid]<dep[lca]||s1<=s2)continue;
else v=fa[v][i];
}
return mp(v,fa[v][]);
}
int main(){
int u,v;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
edg[u].pb(v);
edg[v].pb(u);
}
dfs(,);
scanf("%d",&m);
while(m--){
scanf("%d%d",&u,&v);
int lca=LCA(u,v);
int s=dep[u]+dep[v]-*dep[lca];
if(s&)puts("");
else if(u==v)printf("%d\n",n);
else {
if(dep[u]-dep[lca]==dep[v]-dep[lca]){
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
printf("%d\n",n-sz[u]-sz[v]);
}
else {
pii p=findMid(u,v,lca);
int mid=p.second;
int midson=p.first;
printf("%d\n",sz[mid]-sz[midson]);
}
}
}
return ;
}
Codeforces Round #294 (Div. 2) A and B and Lecture Rooms(LCA 倍增)的更多相关文章
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings
		
题意: 对于26个字母 每个字母分别有一个权值 给出一个字符串,找出有多少个满足条件的子串, 条件:1.第一个字母和最后一个相同,2.除了第一个和最后一个字母外,其他的权和为0 思路: 预处理出sum ...
 - Codeforces Round #294 (Div. 2)
		
水 A. A and B and Chess /* 水题 */ #include <cstdio> #include <algorithm> #include <iost ...
 - Codeforces Round #294 (Div. 2)D - A and B and Interesting Substrings 字符串
		
D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 megaby ...
 - Codeforces Round #294 (Div. 2)C - A and B and Team Training 水题
		
C. A and B and Team Training time limit per test 1 second memory limit per test 256 megabytes input ...
 - Codeforces Round #294 (Div. 2)B - A and B and Compilation Errors 水题
		
B. A and B and Compilation Errors time limit per test 2 seconds memory limit per test 256 megabytes ...
 - Codeforces Round #294 (Div. 2)A - A and B and Chess  水题
		
A. A and B and Chess time limit per test 1 second memory limit per test 256 megabytes input standard ...
 - Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]
		
传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...
 - [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 A and B and Lecture Rooms LCA倍增
		
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
 
随机推荐
- async和await关键词用于定义原生的协程
			
#python为了将语义变得更加明确,就引入了async和await关键词用于定义原生的协程 # async def downloader(url): # return "xxxx" ...
 - MYSQL查找总结
			
a.条件判断where select * from 表 where id > 1 and name != 'alex' and num = 12; select * from 表 where ...
 - lua中的继承
			
做为一个java出身的程序媛,长时间做Lua,重复一些工作后,特别想用继承.其实很简单.因为我有一大部分的场景,背景长的都一样,所以打算做一个父类. 需要注意的是,如果子类有和父类的同名函数,就会被覆 ...
 - [Unity]游戏Inside中的Chromatic Aberration效果学习
			
Chromatic Aberration效果指的是模拟摄像机的拍摄瑕疵导致rgb三个通道的颜色发生了偏移,如 传统的Chromatic Aberration实现往往是基于一个后处理,将rgb采样的坐标 ...
 - 【洛谷 P3809】 【模板】后缀排序
			
题目链接 先占个坑,以后再补. \(SA\)的总结肯定是要写的. 等理解地深入一点再补. #include <cstdio> #include <cstring> const ...
 - 14、char和varchar的区别?
			
就长度来说: ♣ char的长度是不可变的; ♣ 而varchar的长度是可变的,也就是说,定义一个char[10]和varchar[10],如果存进去的是‘csdn’,那么char所占的长度依然为1 ...
 - js作用域与上下文
			
作用域:与调用函数,访问变量的能力有关 作用域分为:局部和全局(在局部作用域里可以访问到全局作用域的变量,但在局部作用域外面就访问不到局部作用里面所设定的变量) 上下文:与this关键字有关 是调用当 ...
 - c#使用selenium+Chromedriver参数配置
			
using System; //添加selenium的引用 using OpenQA.Selenium.PhantomJS; using OpenQA.Selenium.Chrome; using O ...
 - nginx学习时使用EditPuls编辑conf.xml
			
一.Nginx简介:是一个使用c语言开发的高性能的http服务器及反向代理服务器 二.由于在linux环境中使用vim修改配置文件相对困难.为此,我们可以借助EditPlus将虚拟机上面的目录引用到E ...
 - 流程控制--for序列
			
In []: list1 = [,,,] In []: for i in list1: ...: print i ...: In []: for i in list1: print i, ...: / ...