链接: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/倍增法模板)的更多相关文章

  1. poj 1330 LCA (倍增+离线Tarjan)

    /* 先来个倍增 */ #include<iostream> #include<cstring> #include<cstdio> #define maxn 100 ...

  2. POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)

    1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...

  3. LCA(最近公共祖先)——LCA倍增法

    一.前人种树 博客:最近公共祖先 LCA 倍增法 博客:浅谈倍增法求LCA 二.沙场练兵 题目:POJ 1330 Nearest Common Ancestors 代码: const int MAXN ...

  4. poj1470 LCA倍增法

    倍增法模板题 #include<iostream> #include<cstring> #include<cstdio> #include<queue> ...

  5. luogu3379 【模板】最近公共祖先(LCA) 倍增法

    题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...

  6. poj 1330 LCA最近公共祖先

    今天学LCA,先照一个模板学习代码,给一个离线算法,主要方法是并查集加上递归思想. 再搞,第一个离线算法是比较常用了,基本离线都用这种方法了,复杂度O(n+q).通过递归思想和并查集来寻找最近公共祖先 ...

  7. hdu2586 lca倍增法

    倍增法加了边的权值,bfs的时候顺便把每个点深度求出来即可 #include<iostream> #include<cstring> #include<cstdio> ...

  8. 最近公共祖先 LCA 倍增法

    [简介] 解决LCA问题的倍增法是一种基于倍增思想的在线算法. [原理] 原理和同样是使用倍增思想的RMQ-ST 算法类似,比较简单,想清楚后很容易实现. 对于每个节点u , ancestors[u] ...

  9. POJ 1330 LCA裸题~

    POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...

随机推荐

  1. Node.js核心模块-fs文件系统

    fs是file-system的简写,文件系统的意思.在Node中如果想要进行文件操作,就必须引入fs这个核心模块. 引入 const fs = require('fs') fs.readFile(pa ...

  2. git同步代码至github和gitee(码云)

    注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/git-to-g ...

  3. powerdesigner两数据库对比

    1. powerdesigner两数据库对比 1.1. 问题 position属性不一致,但这并不影响两数据库的结构,我们不需要把它当警告显示出来 1.2. 解决办法

  4. 【spring】jdbcTemplate之sql参数注入

    demo @Repository("jdbcDao") public class JdbcTemplateDao { @Autowired private JdbcTemplate ...

  5. 关于c# hashtable的一个注意点

    Hashtable在操作时,一定要注意一点: 当保存值时,如果使用的是字符串作为键,那么在判断是否存在此键时,必须使用字符串来检查,否则,即使是能隐式转换的值也将无法检查到,如: Hashtable ...

  6. STL函数 lower_bound 和 upper_bound 在算法竞赛中的用法

    以前比较排斥这两个函数,遇到需要二分的情景都是手写 \(while(left<=right)\). 这次决定洗心革面记录一下这两个函数的在算法竞赛中的用法,毕竟一般不会导致TLE. 其实百度百科 ...

  7. sift代码实现详解

    1.创建高斯金字塔第-1组 1.1.将源图片转成灰度图 void ConvertToGray(const Mat& src, Mat& dst) { cv::Size size = s ...

  8. Leetcode Week4 Find Minimum in Rotated Sorted Array II

    Question Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeha ...

  9. 专项:Vuejs面试题集合

    参考网络资源:https://segmentfault.com/a/1190000012315822 1.active-class是哪个组件的属性? 答:active-class是vue-router ...

  10. 全网最详细——Testlink在windows环境下搭建;提供环境下载

    参考这篇文章,写的真不错https://www.jianshu.com/p/6c4321de26ea 工具下载 链接:https://pan.baidu.com/s/1_yzCIvsExbfzcRdl ...