【模板】Lca倍增法
Codevs 1036 商务旅行
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=;
int n,m,x,y,ans=,tot=,last[maxn],dep[maxn],p[maxn][];
struct edge{
int to,pre;
}e[maxn<<]; void read(int &k){
k=; int f=; char c=getchar();
while (c<''||c>'')c=='-'&&(f=-),c=getchar();
while (''<=c&&c<='')k=k*+c-'',c=getchar();
k*=f;
}
void add(int x,int y){
e[++tot].to=y;
e[tot].pre=last[x];
last[x]=tot;
}
void dfs(int u,int fa){
dep[u]=dep[fa]+; p[u][]=fa;
for (int i=;i<=log2(dep[u]);i++) p[u][i]=p[p[u][i-]][i-];
for (int i=last[u],to=e[i].to;i;i=e[i].pre,to=e[i].to)
if (to!=fa) dfs(to,u);
}
int lca(int x,int y){
if (dep[x]<dep[y]) swap(x,y);
if (dep[x]>dep[y])
for (int i=log2(dep[x]-dep[y]+);i>=,dep[x]!=dep[y];i--)
if (dep[p[x][i]]>=dep[y]) x=p[x][i];
if (x==y) return x;
for (int i=log2(dep[x]);i>=;i--)
if (p[x][i]!=p[y][i]) x=p[x][i],y=p[y][i];
return p[x][];
}
int main(){
read(n);
for (int i=;i<n;i++){
read(x); read(y);
add(x,y); add(y,x);
}
dfs(,);
read(m); read(x);
for (int i=;i<m;i++){
read(y);
ans+=dep[x]+dep[y]-(dep[lca(x,y)]<<);
x=y;
}
printf("%d\n",ans);
return ;
}
【模板】Lca倍增法的更多相关文章
- LCA(最近公共祖先)——LCA倍增法
一.前人种树 博客:最近公共祖先 LCA 倍增法 博客:浅谈倍增法求LCA 二.沙场练兵 题目:POJ 1330 Nearest Common Ancestors 代码: const int MAXN ...
- POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)
1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...
- luogu3379 【模板】最近公共祖先(LCA) 倍增法
题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...
- poj1470 LCA倍增法
倍增法模板题 #include<iostream> #include<cstring> #include<cstdio> #include<queue> ...
- hdu2586 lca倍增法
倍增法加了边的权值,bfs的时候顺便把每个点深度求出来即可 #include<iostream> #include<cstring> #include<cstdio> ...
- 最近公共祖先 LCA 倍增法
[简介] 解决LCA问题的倍增法是一种基于倍增思想的在线算法. [原理] 原理和同样是使用倍增思想的RMQ-ST 算法类似,比较简单,想清楚后很容易实现. 对于每个节点u , ancestors[u] ...
- POJ 1330(LCA/倍增法模板)
链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: #inc ...
- LCA—倍增法求解
LCA(Least Common Ancestors) 即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 常见解法一般有三种 这里讲解一种在线算法-倍增 首先我们定义fa[u][j ...
- LCA - 倍增法去求第几个节点
You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- 【WIP_S4】栈
创建: 2018/01/15 更新: 2018/01/24 更改标题 [[WIP_S3]堆] => [[WIP_S4]堆] 继续添加内容 更新: 2018/05/13 更改标题 [[WIP_S ...
- jQuery setInterval倒计时精确到毫秒
效果类似于:购物抢购倒计时-->在跳转N多个页面之后,倒计时间仍然正常显示. 思路: 结束时间是固定不变的(endTime),一直在改变的是当下的时间(curTime = new date()) ...
- python 中 str与bytes的转换
# bytes转字符串方式一 b=b'\xe9\x80\x86\xe7\x81\xab' string=str(b,'utf-8') print(string) # bytes转字符串方式二 b=b' ...
- linux学习之路3 文件系统结构
一些有用的定义: linux文件系统为一个倒转的单根树状结构 文件系统的根为"/" linux系统文件严格区分大小写,而windows系统不区分大小写 路径使用"/&qu ...
- 题解报告:hdu 1035 Robot Motion(简单搜索一遍)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- ACM_天涯若比邻(最小与最大相邻素数)
天涯若比邻 Time Limit: 2000/1000ms (Java/Others) Problem Description: 一心想搞ACM的小G最近迷上了数论,特别对于跟“素数”相关的问题特别有 ...
- Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类
Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...
- 268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...
- 2105. [NOIP2015] 信息传递
★☆ 输入文件:2015message.in 输出文件:2015message.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] 有n个同学(编号为1到n)正在 ...
- 关于定位中left和right,top和bottom的权重问题
关于定位中left和right,top和bottom的权重问题 在共同类中设置了定位并且设置了left等定位,如果你引用这个类并加入其他的类中也有left和right等定位,那么你设置的right或是 ...