【POJ 1330 Nearest Common Ancestors】LCA问题 Tarjan算法
题目链接: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()
{
freopen("1330.txt", "r", stdin);
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算法的更多相关文章
- 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题解
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,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- 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 / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
随机推荐
- Binary Search Tree DFS Template
Two methods: 1. Traverse 2. Divide & Conquer // Traverse: usually do not have return value publi ...
- 什么是野指针?(What is a wild pointer?)
未被初始化的变量称为野指针(wild pointer).顾名思义,我们不知道这个指针指向内存中的什么地址,使用不当程序会产生各种各样的问题. 理解下面的例子: int main() { int *p; ...
- shell数组(产生不同的随机数)
#!/bin/bash # declare -a ARRAY read -p "Please input num[1-39]:" EMENUM #对比新生成的随机数是否重复 fun ...
- 《Java程序员面试笔试宝典》之Java程序初始化的顺序是怎样的
在Java语言中,当实例化对象时,对象所在类的所有成员变量首先要进行初始化,只有当所有类成员完成初始化后,才会调用对象所在类的构造函数创建对象. Java程序的初始化一般遵循以下三个原则(以下三原则优 ...
- python之路-模块安装 paramiko
paramiko介绍(全是洋文,看不懂啊,赶紧有道翻译吧,等有朝一日,我去报个华尔街): "Paramiko" is a combination of the esperanto ...
- mysql 自己定义存储过程和触发器
mysql 自己定义存储过程和触发器 --存储过程示范 DROP PROCEDURE IF EXISTS PRO_TEST; CREATE PROCEDURE PRO_TEST(IN NUM_IN I ...
- Android OpenGL库加载过程源码分析
Android系统采用OpenGL绘制3D图形,使用skia来绘制二维图形:OpenGL源码位于: frameworks/native/opengl frameworks/base/opengl 本文 ...
- bash的元字符(下)
` "Esc"键正下方键,替换命令 PS1=`command` {} 在当前shell中运行命令 {command1;command2} | 创建命令间的管道 command1 | ...
- php的一些特殊用法
php ruturn的另一个用法 database.php <?php return array ( 'hostname' => 'localhost', 'database' => ...
- Python学习笔记5(字符串与正则表达式)
1.字符串 1.1字符串的格式化 #格式化语法 "%s" % str1 "%s %s" % (str1,str2) #格式化字符串 str1 = "v ...