Codeforces 519E A and B and Lecture Rooms [倍增法LCA]
题意:
给你一棵有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]的更多相关文章
- 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 ...
- 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 519E A and B and Lecture Rooms
http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直 ...
- 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= ...
- CF 519E(树上倍增求lca)
传送门:A and B and Lecture Rooms 题意:给定一棵树,每次询问到达点u,v距离相等的点有多少个. 分析:按情况考虑: 1.abs(deep[u]-deep[v])%2==1时, ...
- [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)
题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...
- CodeForces 519E 树形DP A and B and Lecture Rooms
给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...
随机推荐
- Java模板引擎 HTTL
新一代java模板引擎典范 Beetl http://www.oschina.net/p/httl HTTL(Hyper-Text Template Language)是一个高性能的开源JAVA模板引 ...
- 触发器 'SA.U_USER_INFO_TRG' 无效且未通过重新验证--Oracle序列
程序开发时报错:触发器 'SA.U_USER_INFO_TRG' 无效且未通过重新验证打开触发器的定义,执行其中的语句,发现序列 U_USER_INFO_SEQ 未定义.什么是序列呢?序列相当于sql ...
- Pop Sequence
题目来源:PTA02-线性结构3 Pop Sequence (25分) Question:Given a stack which can keep M numbers at most. Push ...
- IOS-归档的使用
归档又叫序列化(coding) // 归档NSKeyedArchiver // 解归档NSKeyedUnarchiver // 归档之后的文件 会被加密 // 可以归档的文件.数据--> ...
- THCircularProgressView.h 的使用方法
// // MainViewController.m // fitmiss // // Created by bill on 13-4-11. // Copyright (c) 2013年 lear. ...
- 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory
关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...
- 【转】JDBC为什么要使用PreparedStatement而不是Statement
http://www.importnew.com/5006.html PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedSta ...
- Spring小结
一.环境搭建 创建Maven项目 一般pom.xml会出错,本地若无相应版本的jar包,则无法下载或下载速度非常慢,我的解决方案是,查找本地仓库的jar,修改为本地仓库有的jar即可 pom.xml的 ...
- Linux删除包含特殊符号文件名的文件
今天发现机器上有一文件名为 ~~test 的文件名,欲删除之 ,报错查了下, 发现如下解决方法 假设Linux系统中有一个文件名叫“-test”.如果用户想删除它,按照一般的删除方法在命令行中输入“r ...
- android学习笔记八——SeekBar
SeekBar——拖动条 拖动条(SeekBar)组件与ProgressBar水平形式的显示进度条类似,不过其最大的区别在于,拖动条可以由用户自己进行手工的调节,例如当用户需要调整播放器音量或者电影的 ...