题意:

给你一棵有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. expression encoder 4 安装 出现“已经安排重启您的计算机

    问题: expression encoder 4  安装 出现“已经安排重启您的计算机 解决的办法,注册表数据的修改 开始 运行 regedit HKEY_LOCAL_MACHINE\SYSTEM\C ...

  2. MySQL Show命令的使用

    show tables或show tables from database_name; 解释:显示当前数据库中所有表的名称 show databases; 解释:显示mysql中所有数据库的名称 sh ...

  3. BlueStacks 设置代理服务器 Proxifier指定任意程序的代理服务器

    详见地址: http://www.ccproxy.com/proxifier-tou-ming-dai-li.htm BlueStacks如何使用代理服务器 http://www.360doc.com ...

  4. SwitchyOmega

    SwitchyOmega下载安装地址: http://switchyomega.com/download.html GFWList.bak.txt教程 {"+GFWed":{&qu ...

  5. MockMultipartFile

    org.springframework.mock.webClass MockMultipartFile java.lang.Object org.springframework.mock.web.Mo ...

  6. js跨域--服务器端设置

    首先低版本浏览器是不支持的,我从网上搜的是[IE8.Firefox 3.5 及其以后的版本.Chrome浏览器.Safari 4 等已经实现了] 下面主要是针对服务器端设置Access-Control ...

  7. Linux下访问其他机器的共享

    1.如何查看其他机器上的共享列表? 解答:使用smbclient 客户端,在Linux机器上可以用来查看服务器上的共享资源,也可以向ftp一样,用户可以登陆samba服务器,也可以上传put和下载ge ...

  8. WPF4.5 中的新增功能和增强功能的信息

    本主题包含有关 Windows Presentation Foundation (WPF) 版本 4.5 中的新增功能和增强功能的信息. 本主题包含以下各节: 功能区控件 改善性能,当显示大时设置分组 ...

  9. IntelliJ IDEA中怎么查看文件中所有方法?

    可以使用快捷键ALT + 7打开左侧的Structure查看当前文件中的所有方法.   来自为知笔记(Wiz)

  10. Office导入导出组件权限配置汇总

    NET导出Excel遇到的80070005错误的解决方法: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败,原因是出现 ...