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 ...
随机推荐
- LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解
http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...
- js数组的误解
js数组实际是个残废货,没有关联数组这一说,要实现真正意义上的关联数组只能用对象,那你肯定不服气了,说怎么没有关联数组,我来给你写一个: var arr = []; arr['a'] = 1; arr ...
- 重复代码Duplicated Code---要重构的信号
什么时候需要重构,当你在项目代码里面嗅到这个味道的时候,就要进行重构. 首个介绍的味道是重复代码的味道. 它表现出来的特征是这些: 1.一个类里面,两个函数中,含有相同的代码,类似的代码: ...
- 【BZOJ4816】【SDOI2017】数字表格 [莫比乌斯反演]
数字表格 Time Limit: 50 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Doris刚刚学习了fibonac ...
- python3中处理url异常
import urllib.request import urllib.error url = 'http://c.telunyun.com/Chart/getJsonData?market=1' d ...
- FastDFS介绍和配置过程
由于网站使用nfs共享方式保存用户上传的图片,附件等资料,然后通过apache下载的方式供用户访问,在网站架构初期,使用这种简单的方式实现了静态资源的读写分离,但随着网站数据量的增加,图片服务器渐渐成 ...
- gcc中的内嵌汇编语言(Intel i386平台)
[转]http://bbs.chinaunix.net/thread-2149855-1-1.html 一.声明 虽然Linux的核心代码大部分是用C语言编写的,但是不可避免的其中还是有一部分是用汇 ...
- tenda t402 家庭版 有线路由器
使用快速向导: adsl(拨号)+用户名+密码 路由器后DMZ主机设置简单图解:http://wenku.baidu.com/view/94b9f0768e9951e79b8927ce.html 可 ...
- 64_l5
libsmartcols-2.29.1-2.fc26.x86_64.rpm 13-Feb-2017 07:04 150590 libsmartcols-devel-2.29.1-2.fc26.i686 ...
- C 实现一个简易的Http服务器 (二)
正文 - 直接搞起 C 实现一个简易的Http服务器 很久以前写过一个简易的http服务器, 后面和一个朋友交流, 反思后发现问题不少.在这里简单搞一下. 让其更加简单去表现httpd本质, 弱化协议 ...