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 ...
随机推荐
- C11内存管理之道:智能指针
1.shared_ptr共享智能指针 std::shared_ptr使用引用计数,每个shared_ptr的拷贝都指向相同的内存,在最后一个shared_ptr析构的时候,内存才会释放. 1.1 基本 ...
- CentOS安装JDK环境
一:查看当前系统的java环境 [elsearch@localhost data]$ rpm -qa | grep jdk 二:卸载原有的jdk [elsearch@localhost /]$ yum ...
- onCreateView的一个细节--Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup contaiiner, Bundle savedInstanceState) 在 ...
- 【Foreign】光 [莫比乌斯反演]
光 Time Limit: 10 Sec Memory Limit: 128 MB Description 天猫有一个长方形盒子,长宽分别为A,B. 这个长方形盒子的内壁全部是镜面. 天猫在这个盒子 ...
- Port-knocking 简单教程
0. "port knocking" 如字面意思,类似'敲门',只是这里敲的是'端口',而且需要按照顺序'敲'端口.如果敲击规则匹配,则可以让防火墙实时更改策略.从而达到开关防火墙 ...
- 另类dedecms后台拿shell
遇到一个被阉割的后台,发现直接传shell显然不行. 然后就有了下文 添加一个新广告. 插入一句话木马: --><?php $_GET[c]($_POST[x]);?><!-- ...
- C++之容器
容器,迭代器与容器适配器 所谓容器,即是将最常运用的一些数据结构(data structures)用类模板实现出来,用于容纳特定类型的对象.根据数据在容器中排列的特性,容器可概分为序列式(sequen ...
- 解决TextView多行滑动与NestedScrollView等,滑动冲突,我的解决方案
1.首先要明白,什么时候回TextView处理滑动,什么时候不处理滑动 1.1往上滑动,到达文本底部就不要再处理了,如果往上滑动不在底部则继续TextView滑动 1.2往下滑动,到达文本顶部就不要再 ...
- C C++ 常被人问的问题分析
正文 - 开始了, 直接扯淡 以下都是自己面试中遇到的常见的问题.如有不妥的地方就当见笑了. 哈哈 1. 谈谈你们服务器的架构吧. 分析: 假如这是第一个问题, 你可以走了. 可能各方面原因他不想 ...
- IE7下面iframe滚动条无法用鼠标轮滚 其他浏览器可以
1.让 IFRAME 隐藏滚动条,通常的做法就是在嵌入 IFRAME 的页面的 CSS 中指定以下规则: html, body {overflow: hidden} 2.如果只是想隐藏横向滚 ...