A and B and Lecture Rooms(LCA)
题目描述
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 - 1 corridors 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.
Input
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.
Output
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.
Examples
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
解析:
给出树上两点u,v,找到与u,v距离相等的点的个数
先找到u,v的公共祖先r,然后知道u,v间的距离,中间位置为mid;
当然若dis(u,v)为奇数则无解;
将mid的位置进行讨论:
mid在r上:
mid不在r上:
//毒瘤题
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int>G[];
int n,m;
int size[],grand[][],deep[];
//数据范围、数组范围要注意
void build(int x,int pre)
{
for(int i=;i<=;i++)
{
grand[x][i]=grand[grand[x][i-]][i-];
}
size[x]=;
for(int i=;i<G[x].size();i++)
{
int v=G[x][i];
if(v==pre)continue;
deep[v]=deep[x]+;
grand[v][]=x;
build(v,x);
size[x]+=size[v];
}
}
int lca(int u,int v)
{
if(deep[u]<deep[v])swap(u,v);
int dis=deep[u]-deep[v];
///////////////////////////////////
for(int k=;k<;k++){
if((dis>>k)&){
u=grand[u][k];
}
}
//把u调到和v同一深度
///////////////////////////////////
if(u==v)return u;//当且只当u、v处于一条链时
for(int k=;k>=;k--){
if(grand[u][k]!=grand[v][k]){
u=grand[u][k];
v=grand[v][k];
}
}
return grand[u][];
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);//vector可以...节约代码量
}
build(,);//建树
scanf("%d",&m);//m个询问
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
int r=lca(u,v);
int dis=deep[u]+deep[v]-*deep[r];
if(dis&){printf("0\n");}
if(deep[u]>deep[v])swap(u,v);
else
{
dis/=;
/////////////////////////////////////////////////
int mid=v;
for(int k=;k>=;k--){
if((dis>>k)&) mid=grand[mid][k];
}
/////////////////////////找到 mid
int ans=;
if(mid==r)
{
int preu=u,prev=v;
int du=deep[u]-deep[r];du--;
int dv=deep[v]-deep[r];dv--;
for(int k=;k>=;k--){
if((du>>k)&) preu=grand[preu][k];
if((dv>>k)&) prev=grand[prev][k];
}
ans=n-size[preu]-size[prev];
}
else
{
int prev=v,preu=u;
int dv=deep[v]-deep[mid];
dv--;
for(int k=;k>=;k--){
if((dv>>k)&) prev=grand[prev][k];
}
ans=size[mid]-size[prev];
}
cout<<ans<<endl;
}
}
}
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(LCA,倍增)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud E. A and B and Lecture Rooms A and B are ...
- 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 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点的距离相等 ...
- Codeforces 519 E. A and B and Lecture Rooms
Description 询问一个树上与两点距离相等的点的个数. Sol 倍增求LCA. 一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点. 有些结论很容易证明,如果距离是偶数,那 ...
- Codeforces 519E A and B and Lecture Rooms
http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...
- Codeforces519 E. A and B and Lecture Rooms
传送门:>Here< 题意:询问给出一棵无根树上任意两点$a,b$,求关于所有点$i$,$dist(a,i) = dist(b,i)$的点的数量.要求每一次询问在$O(log n)$的时间 ...
随机推荐
- 秘制牛肉Gamma阶段项目展示
秘制牛肉Gamma阶段项目展示 1.团队成员和个人博客 · 左顺:在项目中主要负责后端开发.个人博客 · 袁勤:精通网页项目开发,前端后端都很强,在完成自己后端任务后也会积极帮助其他人的任务.个人博客 ...
- winform自定义分页控件
1.控件代码: public partial class PagerControl : UserControl { #region 构造函数 public PagerControl() { Initi ...
- Windows Server实例防火墙策略的配置方法
概述 本文介绍在Windows Server实例中,如何配置防火墙策略的方法. 详细描述 配置Windows Server版本的防火墙功能方法,参考如下步骤. 提示:此处以Windows Server ...
- Shell脚本之四 内建命令
所谓 Shell 内建命令,就是由 Bash 自身提供的命令,而不是文件系统中的某个可执行文件. 可以使用 type 来确定一个命令是否是内建命令: [root@localhost ~]# type ...
- linux shell提示输入 输错字符解决方法
linux shell提示输入 输错字符解决方法ctrl+回车 删除单个字符ctrl+u删除光标前全部字符ctrl+k删除光标后全部字符
- Android的开机启动流程
1.Android的开机启动流程 Android的层次框架图,如下所示: 图片清晰地展示了Android的五层架构,从上到下依次是:应用层.应用框架层.库层.运行时层以及Linux内核层.Androi ...
- C中关键字inline用法
一.什么是内联函数 在C语言中,如果一些函数被频繁的调用,不断地用函数入栈,即函数栈,则会造成栈空间或者栈内存的大量消耗,为了解决这个问题,特别的引入了inline关键字,表示为内联函数.栈空间指的是 ...
- Akka-CQRS(8)- CQRS Reader Actor 应用实例
前面我们已经讨论了CQRS-Reader-Actor的基本工作原理,现在是时候在之前那个POS例子里进行实际的应用示范了. 假如我们有个业务系统也是在cassandra上的,那么reader就需要把从 ...
- 【转】AXI_Lite 总线详解
目录: · 1.前言 · 2.AXI总线与ZYNQ的关系 · 3 AXI 总线和 AXI 接口以及 AXI 协议 · 3.1 AXI 总线概述 · 3.2 AXI 接口介绍 ·3.3 AXI 协议概述 ...
- Ubuntu 18 Kubernetes集群的安装和部署 以及Helm的安装
首先说一下我的环境, 我是在windows 10 上面建了一个ubuntu18的虚拟机,同时由于某些原因 不受网络限制, 所以安装比较顺利. Install 1.安装并启用 Docker sudo ...