LCA问题的tarjan解法模板

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;
}
朴素法求解: 先对树进行dfs标出每一个节点的深度,对于查找的两个点先判断在不在同一深度,不在 移到统一深度,然后在往上找
 #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 + 详解博客)的更多相关文章

  1. POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...

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

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

  3. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

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

  4. poj 1330 Nearest Common Ancestors 求最近祖先节点

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

  5. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

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

    <题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...

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

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

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

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

随机推荐

  1. 子Div使用Float后如何撑开父Div

    如果想要撑开父元素可以采用以下方法: 方法一: 父元素设置overflow以及zoom,样式如下: 1 <style> 2   #div1{border:1px solid red;ove ...

  2. Windows7 x64配置 Apache2 + PHP5 + MySQL5

    1:相关软件下载: Apache HTTP Server             版本:(httpd-2.2.25-win32-x86-openssl-0.9.8y) PHP             ...

  3. 准标识符(Quasi-dientifier, QI)

    Quasi-identifier From Wikipedia, the free encyclopedia Quasi-identifiers are pieces of information t ...

  4. 用户 'IIS APPPOOL\***' 登录失败

    用户 'IIS APPPOOL\DefaultAppPool' 登录失败. 我在windows8中安装了iis之后添加了我做的网站打开之后提示用户 'IIS APPPOOL\DefaultAppPoo ...

  5. Beta版冲刺Day1

    会议讨论: 628:        已经成功实现了文件的上传功能,但是按钮的布局有点不好看.未完成的功能有:修改老师信息时候弹出小窗口进行修改. 601:        目前还在解决剩下的问题,比如将 ...

  6. Eclipse导入 appcompat,design兼容包

    从Android studio推出1.0正式版后,就一直在as上开发项目,但是最近要测试一个项目,是eclipse结构,导入as后,是各种报错信息,决定改成eclipse. 其中项目中用到了ppcom ...

  7. Android 横屏不让输入法全屏显示

    记录学习之用 查找资料参考记录的 在源码里进行修改.  frameworks/base/core/Java/Android/inputmethodservice/InputMethodService. ...

  8. 简单Matrix 的方法说明记录

    查找资料加上自己理解  ,简单说明Android中Matrix怎么用(新手有错误的地方,希望指正,主要自己记录学习用的) Matrix包含一个3 X 3的矩阵,专门用于图像变换匹配. Matrix提供 ...

  9. ActiveMQ_安全配置(五)

    如果Activemq不加安全配置,那么任何知道队列所在服务器IP的人都可以发送接收队列消息 安全配置主要是通过配置账号密码增强安全性 参考资料:http://activemq.apache.org/s ...

  10. hibernate-取消关联外键引用数据丢失抛异常的设置@NotFound

    hibernate项目里面配了很多many-to-one的关联,后台在查询数据时已经作了健全性判断,但还是经常抛出对象找不到异常: org.hibernate.ObjectNotFoundExcept ...