POJ 1330(LCA/倍增法模板)
链接:http://poj.org/problem?id=1330
题意:q次询问求两个点u,v的LCA
思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v)
AC代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#include<string>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxbit = ;
const int maxn = 1e4+;
vector<int> G[maxn];
int depth[maxn];
int fa[maxn][maxbit];
int Log[maxn];
int N;
void pre(){
Log[] = -;
Log[] = ,Log[] = ;
for(int i = ;i<maxn;i++) Log[i] = Log[i/] + ;
}
void dfs(int cur,int father){//dfs预处理
depth[cur] = depth[father] + ;//当前结点的深度为父亲结点+1
fa[cur][] = father;//更新当前结点的父亲结点
for(int j = ;(<<j)<=N;j++){//倍增更新当前结点的祖先
fa[cur][j] = fa[fa[cur][j-]][j-];
}
for(int i = ;i<G[cur].size() ;i++){
if(G[cur][i] != father) {//dfs遍历
dfs(G[cur][i],cur);
}
}
}
int LCA(int u,int v){
if(depth[u]<depth[v]) swap(u,v);
int dist = depth[u] - depth[v];//深度差
while(depth[u]!=depth[v]){//把较深的结点u倍增到与v高度相等
u = fa[u][Log[depth[u]-depth[v]]];
}
if(u == v) return u;//如果u倍增到v,说明v是u的LCA
for(int i = Log[depth[u]];i>=;i--){//否则两者同时向上倍增
if(fa[u][i]!=fa[v][i]){//如果向上倍增的祖先不同,说明是可以继续倍增
u = fa[u][i];//替换两个结点
v = fa[v][i];
}
}
return fa[u][];//最终结果为u v向上一层就是LCA
}
int main()
{
int t;
pre();
scanf("%d",&t);
while(t--){
scanf("%d",&N);
int root ;
int in[maxn];
for(int i = ;i<maxn;i++){
G[i].clear() ;
}
memset(in,,sizeof(in));
int u,v;
for(int i = ;i<N-;i++){
scanf("%d%d",&u,&v);
in[v] = ;
G[u].push_back(v);
G[v].push_back(u);
}
for(int i = ;i<=N;i++){//寻树的根结点
if(in[i] == ) {
root = i;
break;
}
}
dfs(root,);
scanf("%d%d",&u,&v);
int ans = LCA(u,v);
printf("%d\n",ans);
}
return ;
}
POJ 1330(LCA/倍增法模板)的更多相关文章
- poj 1330 LCA (倍增+离线Tarjan)
/* 先来个倍增 */ #include<iostream> #include<cstring> #include<cstdio> #define maxn 100 ...
- POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)
1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...
- LCA(最近公共祖先)——LCA倍增法
一.前人种树 博客:最近公共祖先 LCA 倍增法 博客:浅谈倍增法求LCA 二.沙场练兵 题目:POJ 1330 Nearest Common Ancestors 代码: const int MAXN ...
- poj1470 LCA倍增法
倍增法模板题 #include<iostream> #include<cstring> #include<cstdio> #include<queue> ...
- luogu3379 【模板】最近公共祖先(LCA) 倍增法
题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...
- poj 1330 LCA最近公共祖先
今天学LCA,先照一个模板学习代码,给一个离线算法,主要方法是并查集加上递归思想. 再搞,第一个离线算法是比较常用了,基本离线都用这种方法了,复杂度O(n+q).通过递归思想和并查集来寻找最近公共祖先 ...
- hdu2586 lca倍增法
倍增法加了边的权值,bfs的时候顺便把每个点深度求出来即可 #include<iostream> #include<cstring> #include<cstdio> ...
- 最近公共祖先 LCA 倍增法
[简介] 解决LCA问题的倍增法是一种基于倍增思想的在线算法. [原理] 原理和同样是使用倍增思想的RMQ-ST 算法类似,比较简单,想清楚后很容易实现. 对于每个节点u , ancestors[u] ...
- POJ 1330 LCA裸题~
POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...
随机推荐
- js监听滚动结束
使用setTimeout模拟滚动结束 let scrollTimer; document.addEventListener("scroll", () => { clearTi ...
- Ceph集群网络切换
背景:需要对已部署好的Ceph集群切换网络,包含包含公共网络和集群网络 1 关闭所有mon节点的mon服务并修改服务器IP systemctl stop ceph-mon@storage01.serv ...
- swiper快速切换插件(两个综合案例源码)
swiper快速切换插件 swiper.js自己去官网下载哈.先来一个tab切换案例: demo.html <!doctype html> <html> <head> ...
- tensorflow模型的保存与加载
模型的保存与加载一般有三种模式:save/load weights(最干净.最轻量级的方式,只保存网络参数,不保存网络状态),save/load entire model(最简单粗暴的方式,把网络所有 ...
- centos6/7 下升级openssl并安装python3
今天是2019年的最后一天了,看了看自己今年写的随笔就一篇,实在有点少得可怜,就想着趁现在有点时间就再写一篇,^_^ centos6 或者centos 7 python 默认都是安装python 2 ...
- Bubble Sort HDU - 5775 树状数组
//每个数字只会被它后面的比它小的数字影响,且会向右移动相应个数的位置 //比如:6 4 3 5 2 1 .4后面比它小的有 三个,因此它的最右边位置就是当前位置 +3,即5 //如果该数字本身在标准 ...
- Linux c++ 转string模板函数,写文件
#include <iostream> #include <sstream> using namespace std;//转string模板函数 template <cl ...
- laravel上传git如何忽略你不想提交的文件
1.在文件根目录下面有一个文件 .gitignore .gitignore文件用来忽略被指定的文件或文件夹的改动,被记录在.gitignore文件里的文件或文件夹,是无法被git跟踪到的,换句话说,被 ...
- poj1042题解
h [1,16] hours all_v=h*12 intervals n [2,25] lakes fi inital intervals fi-di*v v [0,all_v) 题意,做每件事情的 ...
- 主机名由localhost变成bogon是怎么回事,怎样变回localhost这个名字?
如何解决这个问题修改你的 DNS 为公共 DNS,例如 114.114.114.114 或者谷歌的 8.8.8.8.然后修改你的主机名: sudo hostname localhost 出现这个问题的 ...