题目链接:http://poj.org/problem?id=1330

题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先。

数据范围:n [2, 10000]

思路:从树根出发进行后序深度优先遍历,设置vis数组实时记录是否已被访问。

每遍历完一棵子树r,把它并入以r的父节点p为代表元的集合。这时判断p是不是所要求的u, v节点之一,如果r==u,且v已访问过,则lca(u, v)必为v所属集合的代表元。p==v的情况类似。

我的第一道LCA问题的Tarjan算法,题目只有唯一的一组查询,实现起来非常简洁。

注意题目给树的格式:给出n-1个数对<u, v>,u为v的父节点。因此可以当作有向图用邻接表存储,同时记录各个节点的入度,入度为0的点为树根。

 #include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int MAX_N = ;
int parent[MAX_N];
void init(){
for(int i=; i<MAX_N; i++){
parent[i] = i;
}
}
int find(int x){
if(parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return ;
parent[y] = x;
}
bool same(int x, int y){
return find(x) == find(y);
}
vector<int> G[MAX_N];
int u, v;
int T;
int n;
int vis[MAX_N];
int indeg[MAX_N];
void dfs(int r){
//printf("%d\n", r);
for(int i=; i<G[r].size(); i++){
if(!vis[G[r][i]]){
dfs(G[r][i]);
unite(r, G[r][i]);//孩子合并到父节点
}
}
vis[r] = ; //后序遍历
if(r == u && vis[v]){
printf("%d\n", find(v));
return ;
}else if(r == v && vis[u]){
printf("%d\n", find(u));
return ;
}
}
void lca(){
memset(vis, , sizeof(vis));
init();
int r = ;
for(int i=; i<=n; i++){
if(indeg[i]==){
//printf("root : %d\n", i); //入度为0的是树根
dfs(i);
}
}
}
int main()
{
scanf("%d", &T);
while(T--){
scanf("%d", &n);
memset(indeg, , sizeof(indeg));
for(int i=; i<MAX_N; i++) G[i].clear();
for(int i=; i<n-; i++){
scanf("%d%d", &u, &v);
G[u].push_back(v);//有向图
indeg[v]++;
}
scanf("%d%d", &u, &v);
lca();
}
return ;
}

POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)的更多相关文章

  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 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  3. POJ 1330 Nearest Common Ancestors LCA题解

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

  4. poj 1330 Nearest Common Ancestors lca 在线rmq

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

  5. poj 1330 Nearest Common Ancestors LCA

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

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

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

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

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

  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 / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

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

  10. 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. 003-pro ant design 前端权限处理-支持URL参数的页面

    前天需要增加MD5引用 https://www.bootcdn.cn/blueimp-md5/ 1.修改权限文件(CheckPermissions.js)使用自定义权限 2.配置异常页面 2.1.创建 ...

  2. pattern.define注意事项

    1.PAT:最好不要所有的关键字后面都用|,导致整条pattern变成无限统配.

  3. 十步完全理解SQL(转载)

    1. SQL 是一种声明式语言 首先要把这个概念记在脑中:“声明”. SQL 语言是为计算机声明了一个你想从原始数据中获得什么样的结果的一个范例,而不是告诉计算机如何能够得到结果.这是不是很棒? (译 ...

  4. [redis]redis常用

    https://redis.io/topics/quickstart $ redis-cli ping PONG redis-server is the Redis Server itself. re ...

  5. (转)深度教程:POS和POW全解析

    如果你是一名资深的比特币矿工或商人,你一定听说过POW和POS,但是当前在国内,却几乎没有人明白这到底是什么意思,也几乎没有相关的中文资料,小编吐血撰写,是为了让大家更好理解这些概念.   读完本文, ...

  6. centos7 下 apache nginx squid https正向代理 代理服务器

    apache yum install httpd mod_ssl -y vim /etc/httpd/conf.d/ssl.conf Listen https <VirtualHost *:&g ...

  7. winrar自解压释放路径详解

    因为在 WINRAR 的帮助文件中没有对自解压路径和系统的环境变量之间作说明,所以,很多人只知道,其自解压路径可以智能定位到系统的 PROGRAM FILES 目录,而不知道它其实还可以智能定位到系统 ...

  8. 【cocos2d-js官方文档】事件分发监听机制(摘录)

    简介 游戏开发中一个很重要的功能就是交互,如果没有与用户的交互,那么游戏将变成动画,而处理用户交互就需要使用事件监听器了. 总概: 事件监听器(cc.EventListener) 封装用户的事件处理逻 ...

  9. eclipse 把鼠标指针放在错误的语句上 提示快速修正 不见了的解决方法

    Window->Preferences->Java->Editor->Hovers 将[Combined Hover]选择即可,如果第一个[Variable Values]已经 ...

  10. Jenkins. 安装过程中出现一个错误: No such plugin: cloudbees-folder

    安装过程中出现一个错误: No such plugin: cloudbees-folder 安装插件,有时候会报类似的错误:An error occurred during installation: ...