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 ...
随机推荐
- Confluence 6 生产环境备份策略
如果你是下面的情况,Confluence 的自动每日 XML 备份可能适合你: 正在评估使用 Confluence 你对数据库的管理并不是非常熟悉同时你的 Confluence 安装实例的数据量并不大 ...
- python之线程同步
lock与rlock 使用lock不能连续两次获取锁,获取锁必须先释放锁.但是在一个线程中调用另一个函数时,在该函数中要继续操作共享的数据,这时获取锁就相当于连续执行两次获取锁,所以lock就不适用该 ...
- eclipse c++11 cmake gnuradio
承接之前的脚本.修改一下这个脚本的代码就可以让eclipse使用C++11了 #!/bin/sh echo "creat_debug for sdk" echo "mkd ...
- 【python】多进程共享变量
有一个字典变量,需要在多个进程间共享 使用Manager, 下面是一个小例子. 注意使用json前需要将类型转换. #!/usr/bin/python # coding=utf-8 import js ...
- NMT 机器翻译
本文近期学习NMT相关知识,学习大佬资料,汇总便于后期复习用,有问题,欢迎斧正. 目录 RNN Seq2Seq Attention Seq2Seq + Attention Transformer Tr ...
- tp3.2 事务
public function exchangeTransfer($user_id, $type, $money, $config, $other_id = 0) { $r['code'] = ERR ...
- 什么是 TCP 拆、粘包?如何解决(Netty)
前言 记得前段时间我们生产上的一个网关出现了故障. 这个网关逻辑非常简单,就是接收客户端的请求然后解析报文最后发送短信. 但这个请求并不是常见的 HTTP ,而是利用 Netty 自定义的协议. 有个 ...
- stream to byte[], byte[] to srting
byte[] myBinary = new byte[paramFile.Length]; paramFile.Read(myBinary, , (int)paramFile.Length); str ...
- 首次使用idea步骤
修改代码提示快捷键 默认是ctrl+空格,这个会跟中英文切换的快捷键冲突. 我这里改成了alt+/ tomcat的配置
- Visual Studio上编译ncnn
prerequisite 是为了在PC上熟悉ncnn的基本代码,所以用Visual Studio来配置的. 期间用过VS2013(update5)/VS2015/VS2017,反正都是基于CMake生 ...