POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
LCA问题 详细
1、二叉搜索树上找两个节点LCA
public int query(Node t, Node u, Node v) {
int left = u.value;
int right = v.value; //二叉查找树内,如果左结点大于右结点,不对,交换
if (left > right) {
int temp = left;
left = right;
right = temp;
} while (true) {
//如果t小于u、v,往t的右子树中查找
if (t.value < left) {
t = t.right; //如果t大于u、v,往t的左子树中查找
} else if (t.value > right) {
t = t.left;
} else {
return t.value;
}
}
}
2、二叉树上找两个节点
node* getLCA(node* root, node* node1, node* node2)
{
if(root == null)
return null;
if(root== node1 || root==node2)
return root; node* left = getLCA(root->left, node1, node2);
node* right = getLCA(root->right, node1, node2); if(left != null && right != null) // 两个点在root的左右两边,就是root了
return root;
else if(left != null) // 哪边不空返回哪边
return left;
else if (right != null)
return right;
else
return null;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int Max = ;
int t, n, first, second, root;
vector<int> G[Max];
int indegree[Max], depth[Max], father[Max];
void inputTree()
{
for (int i = ; i <= n; i++)
{
G[i].clear();
father[i] = ;
indegree[i] = ;
depth[i] = ;
}
int u, v;
for (int i = ; i < n; i++)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
indegree[v]++;
father[v] = u;
}
scanf("%d%d", &first, &second);
for (int i = ; i <= n; i++)
{
if (indegree[i] == )
{
root = i;
break;
}
}
}
void dfs_depth(int u, int dep)
{
depth[u] = dep;
int Size = G[u].size();
for (int i = ; i < Size; i++)
{
dfs_depth(G[u][i], dep + );
}
}
int find_ancestor()
{
while (depth[first] > depth[second])
{
first = father[first];
}
while (depth[first] < depth[second])
{
second = father[second];
}
while (first != second) // 这样直接返回first
{
first = father[first];
second = father[second];
}
return first;
}
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
inputTree();
dfs_depth(root, );
printf("%d\n", find_ancestor());
}
return ;
}
tarjan + 并查集 解法:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int Max = ;
int t, n, first, second, root;
vector<int> G[Max], querry[Max];
int indegree[Max], father[Max], vis[Max];
void inputTree()
{
for (int i = ; i <= n; i++)
{
G[i].clear();
querry[i].clear();
father[i] = i;
indegree[i] = ;
vis[i] = ;
}
int u, v;
for (int i = ; i < n; i++)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
indegree[v]++;
}
scanf("%d%d", &first, &second);
querry[first].push_back(second);
querry[second].push_back(first);
for (int i = ; i <= n; i++)
{
if (indegree[i] == )
{
root = i;
break;
}
}
}
int find_father(int x)
{
if (x == father[x])
return x;
return father[x] = find_father(father[x]);
}
void unionSet(int x, int y)
{
x = find_father(x);
y = find_father(y);
if (x != y)
father[y] = x;
}
void tarjan(int x)
{
int Size = G[x].size();
for (int i = ; i < Size; i++)
{
int v = G[x][i];
tarjan(v);
unionSet(x, v);
}
vis[x] = ;
/*
if (x == first && vis[second])
printf("%d\n", find_father(second));
else if (x == second && vis[first])
printf("%d\n", find_father(first));
*/
Size = querry[x].size();
for (int i = ; i < Size; i++)
{
if (vis[querry[x][i]])
{
printf("%d\n", find_father(querry[x][i]));
return;
}
} }
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
inputTree();
tarjan(root);
}
return ;
}
POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)的更多相关文章
- POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- 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 ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- poj 1330 Nearest Common Ancestors 求最近祖先节点
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37386 Accept ...
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- POJ 1330 Nearest Common Ancestors (模板题)【LCA】
<题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...
- 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 (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
随机推荐
- 柯尔莫可洛夫-斯米洛夫检验(Kolmogorov–Smirnov test,K-S test)
柯尔莫哥洛夫-斯米尔诺夫检验(Колмогоров-Смирнов检验)基于累计分布函数,用以检验两个经验分布是否不同或一个经验分布与另一个理想分布是否不同. 在进行cumulative probab ...
- 记一次使用命令行启动部署在tomcat上的应用
在Eclipes进行程序开发完成后,一般都会直接在Eclipse部署启动,其中的一些启动参数设置都会在其中进行,若用命令行启动,则需要手动配置. 程序开发完成后打成的war包,需要部署到Tomcat应 ...
- auto refresh iframe
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- Jquery Mobile中pageinit等函数执行两次的问题【终极解决】
当禁用了jqueryMobile的ajax后,初始化函数如pageinit和pageshow等函数,都会执行两次.document.ready函数也会执行两次. 当然我们可以用一个变量记录是否已经执行 ...
- js 基础(一)
<!--最近需要用到js相关的知识 就把在W3cSchool 下学到的东西做个笔记,方便以后再看 --><!DOCTYPE html> <html> <hea ...
- C#利用iComparable接口实现List排序
List<T>类可以使用Sort()方法对元素排序. Sort()方法定义了几个重载方法,分别是 public void List<T>.Sort(),不带有任何参数的Sor ...
- SpringMVC学习--数据回显
简介 表单提交失败需要再回到表单页面重新填写,原来提交的数据需要重新在页面上显示. 简单数据类型 对于简单数据类型,如:Integer.String.Float等使用Model将传入的参数再放到req ...
- python中的字符数字之间的转换函数
int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) ...
- jsp 中的js 与 jstl 运行的先后顺序
在jsp 中运行下面的代码,结论是:js 中可以使用 标签,js 的注释 对标签无效-- 有知道原理的吗<c:set var="flag" value="false ...
- native2ascii 国际资源文件编码
将common_zh_CN.properties 转换为 本地文件 common_zh_CN_src.properties native2ascii -reverse common_zh_CN.pro ...