题意:

给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的。树上所有边的边权是1。

思路:

很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分情况讨论。

一开始困扰我的问题是如果lca不是正中间的点,如何在比较低的复杂度的层面上求解中点。

倍增法lca不光可以在logn的时间复杂度内查询某两个点的lca,还可以实现在logm的时间复杂度能查询某个节点的第m个父亲节点。

算法的核心是用二进制的运算来实现查询。

#include<bits/stdc++.h>
#define N 100050
using namespace std;
int n;
struct edge{
int id;
edge *next;
};
edge edges[N<<];
edge *adj[N];
int ednum;
int dep[N],rt[][N],siz[N];
inline void add_Edge(int a,int b){
edge *tmp=&edges[ednum++];
tmp->id=b;
tmp->next=adj[a];
adj[a]=tmp;
}
void dfs(int pos,int deep){
dep[pos]=deep;
siz[pos]=;
for(edge *it=adj[pos];it;it=it->next){
if(!dep[it->id]){
rt[][it->id]=pos;
dfs(it->id,deep+);
siz[pos]+=siz[it->id];
}
}
}
void prelca(){
for(int i=;i<=;i++){
for(int j=;j<=n;j++){
rt[i][j]=rt[i-][j]==-?-:rt[i-][rt[i-][j]];
}
}
}
int LCA(int u,int v){
if(dep[u]<dep[v])swap(u,v);
for(int i=;i<;i++){
if((dep[u]-dep[v])>>i&){
u=rt[i][u];
}
}
if(u==v)return u;
for(int i=;i>=;i--){
if(rt[i][u]!=rt[i][v]){
u=rt[i][u];
v=rt[i][v];
}
}
return rt[][u];
}
int jump(int pos,int num){
for(int i=;i<;i++){
if(num>>i&){
pos=rt[i][pos];
}
}
return pos;
}
void solve(int u,int v){
if(u==v){
printf("%d\n",n);
return;
}
if(max(dep[u],dep[v])-min(dep[u],dep[v])&){
printf("0\n");
return;
}
int anc=LCA(u,v);
//printf("anc=%d\n",anc);
if(dep[u]==dep[v]){
int ans=n;
ans-=siz[jump(u,dep[u]-dep[anc]-)];
ans-=siz[jump(v,dep[u]-dep[anc]-)];
printf("%d\n",ans);
}
else{
int l=dep[u]+dep[v]-*dep[anc];
if(dep[u]<dep[v])swap(u,v);
int ans=siz[jump(u,l/)];
ans-=siz[jump(u,l/-)];
printf("%d\n",ans);
}
}
int main()
{
scanf("%d",&n);
memset(rt,-,sizeof(rt));
for(int i=;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
add_Edge(a,b);
add_Edge(b,a);
}
dfs(,);
prelca();
//printf("*%d\n",jump(2,0));
int m;
scanf("%d",&m);
for(int i=;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
solve(a,b);
}
return ;
}
/*
5
1 5
1 2
2 3
2 4
5
1 5
2 5
1 1
2 2
3 4
*/

Codeforces 519E A and B and Lecture Rooms [倍增法LCA]的更多相关文章

  1. 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 ...

  2. codeforces 519E A and B and Lecture Rooms LCA倍增

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  3. 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 ...

  4. Codeforces 519E A and B and Lecture Rooms

    http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...

  5. 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 ...

  6. [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= ...

  7. CF 519E(树上倍增求lca)

    传送门:A and B and Lecture Rooms 题意:给定一棵树,每次询问到达点u,v距离相等的点有多少个. 分析:按情况考虑: 1.abs(deep[u]-deep[v])%2==1时, ...

  8. [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)

    题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...

  9. CodeForces 519E 树形DP A and B and Lecture Rooms

    给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...

随机推荐

  1. mysql初学

    本文基于mysql5.1编写 1.创建表: ) ),age ) '); 2.删除表: drop table customer; 3.部分列插入元素: insert into customer(mid, ...

  2. html之内联标签a

    a标签:定义超链接,用于从一个页面链接到另一个页面,最重要的属性href,如果没有href属性,则不可以使用如下属性:download,hreflang,media,rel,target,type. ...

  3. 安卓模拟器安装apk,上网

    1.首先找到安装安卓模拟器的文件夹: 2.目录:D:\Java\Android-all\platform-tools 3.运行cmd命令:adb install D:\Java\Android-all ...

  4. iMac一体机安装苹果和Win7双系统

    前几天,有个客户说有一苹果的一体机,想装苹果和Win7双系统.约好了时间,带上工具就过去了.去的路上,用手机上网查了一下苹果电脑装双系统的过程.虽然以前也有给苹果的电脑安装过双系统,但次数不多而且时间 ...

  5. 连接数据库的URL等于多少?

    JDBC编程步骤如下: 1.加载驱动 Class.forname(driverClass); 比如:加载MySQL的驱动 Class.forname("com.mysql.jdbc.Driv ...

  6. 冒泡算法C#

    冒泡算法C# namespace数组排序 { classProgram { staticvoidMain(string[]args) { inttemp=; ,,,,,,,,}; #region该段与 ...

  7. 从官方下载 Bootstrap 版本 并写 第一个页面

    从官方下载  Bootstrap 版本 页面内容参考自 http://www.cnblogs.com/sanjuantianshu/p/3935120.html bootstrap-3.2.0.zip ...

  8. 杀死future处理的阻塞线程

    public class Test{ public static void main(String[] args){ ExecutorService pool = Executors.newFixed ...

  9. NeHe OpenGL教程 第六课:纹理映射

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  10. Oracle 数据库中不同事务并发访问的问题

    现象 以SQL/Helper为例,打开不同的SQL窗口,对同一个表格进行操作,如下所示. 窗口1:当执行更新任务.紧接着执行查询时获得一组查询结果.结果是对的. 窗口2:而在另外一个SQL查询窗口中执 ...