POJ 1330 Nearest Common Ancestors (模板题)【LCA】
<题目链接>
题目大意:
给出一棵树,问任意两个点的最近公共祖先的编号。
解题分析:
LCA模板题,下面用的是树上倍增求解。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
;
const int INF = 0x3f3f3f3f;
struct Edge{
int to, next;
}edge[N<<];
int n,cnt, head[N], in[N];
];
void add_edge(int v, int u){
edge[cnt].to = u, edge[cnt].next = head[v], head[v] = cnt++;
}
void dfs(int u, int fa){ //得到所有节点的深度
; i = edge[i].next){
int v = edge[i].to;
if(v == fa) continue;
if(!dep[v]){
dep[v] = dep[u] + ;
f[v][] = u;
dfs(v, u);
}
}
}
void init(){ //树上倍增预处理
; (<<j) <= n; j++)
; i <= n; i++)
f[i][j] = f[f[i][j-]][j-];
}
int LCA(int v, int u){
if(dep[v] < dep[u])swap(v, u); //v为深度更深的节点
int d = dep[v] - dep[u];
; (d>>i) != ; i++)
) v = f[v][i]; //以上的操作是处理较深的节点,使两节点深度一致
if(v == u) return v; //如果深度一致时,两节点相同,那么直接返回即可
;i>= ;i--)
if(f[v][i] != f[u][i]) //跳2^i步不一样,就跳,否则不跳
v = f[v][i],u = f[u][i]; //两点一起向上跳2^i步
]; //经证明,上述操作做完,两点的LCA都在上一层,所以再走一步即可
}
int main(){
int t, a, b;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
cnt = ;
memset(head, -, sizeof head);
memset(, sizeof in);
; i < n; i++){
scanf("%d%d", &a, &b);
add_edge(a, b);
in[b]++;
}
memset(dep, , sizeof dep);
int root;
; i <= n; i++)
if(!in[i]) root = i;
dep[root] = ;
dfs(root, -);
init(); //注意这里init要放在dfs后面,因为f[i][0]需要通过dfs预处理得到
scanf("%d%d", &a, &b);
printf("%d\n", LCA(a, b));
}
;
}
2018-10-18
POJ 1330 Nearest Common Ancestors (模板题)【LCA】的更多相关文章
- 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/DFS
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19919 Accept ...
- POJ 1330 Nearest Common Ancestors(裸LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39596 Accept ...
- POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- poj 1330 Nearest Common Ancestors 裸的LCA
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- 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 ...
随机推荐
- 基于AD5663的UV灯电压控制
在开发臭氧发生器的时,我们使用UV灯来实现臭氧的产生.而UV灯的强度决定了臭氧产生的浓度,UV灯的光强则与其控制电压密切相关.所以我们要控制产生的臭氧的浓度就需要调节其控制电压.我们选择了AD5663 ...
- Confluence 6 修改默认空间标识图片
空间标识图片在边栏上的站点目录(Sites Directory)中作为图标进行显示.默认的空间标识图片将会应用到所有的空间中,如果你没有自定义的空间标识被定义的话,请查看 Configure the ...
- 剑指offer 二叉搜索树和双向链表
剑指offer 牛客网 二叉搜索树和双向链表 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 18:58:36 2019 ...
- 开启Java之旅
学习应用系统的服务器开发,也许并不算什么“旅行”,也不会那么‘愉快’.但是,我希望这次能够同以往有所不同,更加努力地学习J2EE. 从2月份开始,从事web前端开发,并在公司的的项目中,独立完成了4个 ...
- tensorflow:验证码的识别(上)
验证码的识别 主要分成四个部分:验证码的生成.将生成的图片制作成tfrecord文件.训练识别模型.测试模型 使用pyCharm作为编译器.本文先介绍前两个部分 验证码的识别有两种方法: 验证码识别方 ...
- MyBatis-Plus工具快速入门
MyBatis-Plus官方文档:http://mp.baomidou.com/#/quick-starthttp://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A ...
- Centos7安装OpenJDK8
https://blog.csdn.net/kanbe_kotori/article/details/70948430
- golang 中操作nsq队列数据库
首先先在本地将服务跑起来,我用的是docker-compose ,一句话6666 先新建一个docker-compose.yml version: '2' services: nsqlookupd: ...
- 为什么在球坐标系中,sinTheta2=std::max(T(0), 1 - cosTheta(w) * cosTheta(w));
球坐标系中,计算sin2θ时,采用的是如下公式,感觉不理解为什么要搞一个max函数,直接1 - cosTheta(w) * cosTheta(w)不行吗,另外,即使要用max,max的第一个参数应该是 ...
- Hankson 的趣味题
题解: 硬是把一道傻逼题写到了200行.. 长长的模板就有70行.. 由于我没有做的时候觉得并不保证$a1|a0$ $b0|b1$ 然后就加了很多特判.. 我的做法就是暴力分解质因数 T*sqrt(n ...