POJ.1330 Nearest Common Ancestors (LCA 倍增)
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 倍增)的更多相关文章
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- POJ 1330 Nearest Common Ancestors LCA题解
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19728 Accept ...
- poj 1330 Nearest Common Ancestors lca 在线rmq
Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
随机推荐
- python多线程创建与使用(转)
原文:http://codingpy.com/article/python-201-a-tutorial-on-threads/ 创建多线程 创建多线程主要有2种方式. 使用threading.Thr ...
- Log4j简单配置解析
log4j.rootLogger=ERROR, stdoutlog4j.logger.tk.mybatis.simple.mapper=TRACElog4j.appender.stdout=org.a ...
- Netty源码分析第5章(ByteBuf)---->第5节: directArena分配缓冲区概述
Netty源码分析第五章: ByteBuf 第五节: directArena分配缓冲区概述 上一小节简单分析了PooledByteBufAllocator中, 线程局部缓存和arean的相关逻辑, 这 ...
- VSCode配合ESLint自动修复格式化
开发Vue或者React的项目的时候,我们经常用到ESLint进行代码的校验,当代码出现不符合规范的格式的时候,ESLint会在控制台提示相关的异常信息. ESLint极大的提高了团队代码的一致性和规 ...
- unload没有用
今天下午测试了unload这个事件包括beforeunload <script type="text/javascript"> window.addEventListe ...
- 软件项目第一次Sprint评价
1.9-652 首先该小组在第一个冲刺阶段的目标还是很明确的,按照这个目标去完成并实现任务,最后也确实是实现了.通过展示目前已经完成了界面的设计及实现.初步的游戏人物设定.生命值设定.除了完成的这些, ...
- Leetcode题库——4.寻找两个有序数组的中位数
@author: ZZQ @software: PyCharm @file: findMedianSortedArrays.py @time: 2018/10/10 19:24 说明:给定两个大小为 ...
- 其实servlet就是一种mvc设计思想的一种实现
看图,不说话
- 《TCP/IP 详解 卷1:协议》第 5 章:Internet 协议
IP 是 TCPIP 协议族中的核心协议.所有 TCP.UDP.ICMP.IGMP 数据都通过 IP 数据包(又称为 packet)来传输.IP 的英文名为 Internet Protocol,是互联 ...
- iOS UIView性能最优的设计圆角并且绘制边框颜色
//以给cell切圆角为例- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollection ...