POJ.1330 Nearest Common Ancestors (LCA 倍增)

题意分析

给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b。接下来给出xy,求出xy的lca节点编号。

LCA裸题,用倍增思想。

代码总览

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define nmax 80520
#define demen 25
using namespace std;
int fa[nmax][demen],head[nmax],dep[nmax];
int n,m,tot = 0;
struct node{
int to;
int next;
int w;
}edge[nmax];
void add(int u, int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void dfs(int rt,int f){
fa[rt][0] = f;
for(int i = 1;i<=20;++i){
fa[rt][i] = fa[fa[rt][i-1]][i-1];
}
for(int i = head[rt];i!=-1;i = edge[i].next){
int nxt = edge[i].to;
if(nxt != f){
dep[nxt] = dep[rt] + 1;
dfs(nxt,rt);
}
}
}
int lca(int x, int y){
int X = x,Y=y;
if(dep[x] < dep[y]) swap(x,y);
int dis = dep[x] - dep[y];
for(int i = 20;i>=0;--i){
if((1<<i) & dis)
x = fa[x][i];
}
if(x == y) return(x);
for(int i = 20;i>=0;--i){
if(fa[x][i] != fa[y][i]){
x = fa[x][i],y = fa[y][i];
}
}
return(fa[x][0]);
}
void init(){
memset(fa,0,sizeof fa);
memset(head,-1,sizeof head);
memset(dep,0,sizeof dep);
tot = 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
int n,u,v;
scanf("%d",&n);
int root = 0;
for(int i = 0;i<n-1;++i){
scanf("%d %d",&u,&v);
if(root == 0) root = u;
add(u,v);
add(v,u);
}
dep[root] = 1;
dfs(root,0);
scanf("%d %d",&u,&v);
printf("%d\n",lca(u,v));
}
return 0;
}

POJ.1330 Nearest Common Ancestors (LCA 倍增)的更多相关文章

  1. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

  2. POJ 1330 Nearest Common Ancestors LCA题解

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19728   Accept ...

  3. poj 1330 Nearest Common Ancestors lca 在线rmq

    Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...

  4. poj 1330 Nearest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...

  5. POJ 1330 Nearest Common Ancestors(LCA模板)

    给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...

  6. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

  7. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  8. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  9. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

随机推荐

  1. 正确配置 debian squeeze apt 源

    本想在 Debian Squeeze 上安装一些依赖,没想到刚执行 apt-get update ,就出现这样的错误信息. W: GPG error: http://mirrors.163.com s ...

  2. Ruby知识点二:类

    1.追查对象是否属于某个类时,使用is_a?方法  追查某个对象属于哪个类时,使用class方法 判断某个对象是否属于某个类时,使用instance_of?方法 判断类是否包含某个模块,使用inclu ...

  3. windows 7 php 7.1 命令行 执行 中文文件名 的PHP文件

    在PHP5.6时代直接执行 php.exe  文件.php 是没有的这个问题 在win下的命令行中 活动代码页命令 chcp 修改 chcp 936  //gbk chcp 65001 //utf-8 ...

  4. SharpDevelop 笔记

    1. 下载地址: http://jaist.dl.sourceforge.net/project/sharpdevelop/ 2. 使用 VS2012 去掉编译不通过的 Test ,其它可以运行调试. ...

  5. php 数组去重

    php 数组去重 数组中重复项的去除 2010-07-28 15:29 一维数组的重复项: 使用array_unique函数即可,使用实例如下: <?php                    ...

  6. 微信小程序——节奏练耳 宣传页

    节奏练耳是什么? 节奏练耳小程序是一款听音练习节奏的交互式小程序.节奏练耳第一大节是辨认六种音符的练习,剩余九大节的练习题中播放的音频是将时值长短不一的音符组合在一起,配合相应的节奏图片,以提高辨认节 ...

  7. Team Member Introduction and Division of Work

    Team leader Name:宋天舒 Student Number:12061166 Interested In: Information safety. Responsible For: Des ...

  8. OO终章--总结博客

    一.测试与正确性论证的比较 从方法上看,测试是使用大量测试样例来覆盖测试代码,从而能够检测代码的实现是否正确,功能是否完善.而正确性论证是使用代码的规格和逻辑进行严密的推论和证明,从而验证代码的实现正 ...

  9. fcn模型训练及测试

    1.模型下载 1)下载新版caffe: https://github.com/BVLC/caffe 2)下载fcn代码: https://github.com/shelhamer/fcn.berkel ...

  10. 结对&团队之1715|K班取经

    声明:同学请勿抄袭,追责莫要怪我: 因为暂时闲着没事,就翻阅学长学姐的博客找找灵感,个人觉得应该还有人和我一样对软工实践未来的一大段路还很天真,包括目前的结对作业和团队组队也很迷路,于是写下这篇博客提 ...