Nearest Common Ancestors

本文深入讲解LCA(最近公共祖先)算法,通过实例解析Tarjan思想在深搜树中的应用,展示如何通过并查集优化算法,降低时间复杂度至O(n+q),适用于有根树结构的数据查询。

Nearest Common Ancestors

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 38307 Accepted: 19085

Description

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

 
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is. 

For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y. 

Write a program that finds the nearest common ancestor of two distinct nodes in a tree. 
 

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.

Output

Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.

Sample Input

2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5

Sample Output

4
3

Source

Taejon 2002

这是我的第一篇LCA文章,可以看看这篇关于LCA算法简介

LCA 最近公共祖先

 

这篇博客写的非常不错,我就是看这个学会的。

第一次写最近公共祖先问题,用的邻接表指针。

对于一棵有根树,就会有父亲结点,祖先结点,当然最近公共祖先就是这两个点所有的祖先结点中深度最大的一个结点。

       0

       |

       1

     /   \

   2      3

比如说在这里,如果0为根的话,那么1是2和3的父亲结点,0是1的父亲结点,0和1都是2和3的公共祖先结点,但是1才是最近的公共祖先结点,或者说1是2和3的所有祖先结点中距离根结点最远的祖先结点。

在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好的处理技巧就是在回溯到结点u的时候,u的子树已经遍历,这时候才把u结点放入合并集合中,这样u结点和所有u的子树中的结点的最近公共祖先就是u了,u和还未遍历的所有u的兄弟结点及子树中的最近公共祖先就是u的父亲结点。以此类推。。这样我们在对树深度遍历的时候就很自然的将树中的结点分成若干的集合,两个集合中的所属不同集合的任意一对顶点的公共祖先都是相同的,也就是说这两个集合的最近公共最先只有一个。对于每个集合而言可以用并查集来优化,时间复杂度就大大降低了,为O(n + q),n为总结点数,q为询问结点对数。

另外Tarjan解法,是一个离线算法,就是说它必须将所有询问先记录下来,再一次性的求出每个点对的最近公共祖先,只有这样才可以达到降低时间复杂度。另外还有一个在线算法,有待学习

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int N=100005;
int n;
vector<int> vec[N];
int pre[N];
bool vis[N];
bool root[N];
int u,v;
int Find(int x)
{
    return x==pre[x]?x:Find(pre[x]);
}
void mix(int x,int y)
{
    int xx=Find(x);
    int yy=Find(y);
    if(xx!=yy)
    {
        pre[yy]=xx;//不可搞错顺序,xx是yy的祖先
    }
}
void Init()
{
    for(int i=0; i<=n; i++)
    {
        vec[i].clear();
        pre[i]=i;
        root[i]=true;
        vis[i]=false;
    }
}
void Tarjan(int x)
{
    for(int i=0; i<(int)vec[x].size(); i++)
    {
        Tarjan(vec[x][i]);
        mix(x,vec[x][i]);
    }
    vis[x]=true;
    if(u==x&&vis[v]==true)
    {
        printf("%d\n",Find(v));
        return ;
    }
    if(v==x&&vis[u]==true)
    {
        printf("%d\n",Find(u));
        return ;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        Init();
        for(int i=0; i<n-1; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            vec[a].push_back(b);
            root[b]=false;
        }
        scanf("%d%d",&u,&v);
        for(int i=1; i<=n; i++)
        {
            if(root[i]==true)
            {
                Tarjan(i);
                break;
            }
        }
    }
    return 0;
}

 

### Nearest Algorithm or Implementation in Programming In programming, the concept of 'nearest' is often associated with algorithms that deal with proximity or similarity between data points. One common application involves finding the nearest neighbor within a set of points based on some distance metric such as Euclidean distance. A widely used approach for this purpose includes **k-nearest neighbors (KNN)** which falls under supervised learning methods where it can be applied both to classification and regression tasks [^2]. For instance: - In classification problems using KNN, an object's class membership is determined by majority voting among its k closest training samples. Another relevant area could involve string matching techniques like Levenshtein Distance calculation when considering textual differences; however specific implementations depend heavily upon context including language choice etcetera but generally follow similar principles regarding measuring closeness/distance metrics appropriately adjusted according domain requirements whether numerical vectors spaces strings sequences et cetera . Here’s how you might implement basic version knn classifier python utilizing euclidian distances : ```python import numpy as np def classify_knn(data_points , query_point,k=3): """Classify new point via KNN.""" distances = [] for index,point in enumerate(data_points): dist=np.linalg.norm(np.array(point)-np.array(query_point)) distances.append((dist,index)) sorted_distances=sorted(distances,key=lambda x:x[0]) top_indices=[i for d,i in sorted_distances[:k]] classes=[data_points[i][1]for i in top_indices ] prediction=max(set(classes),key=classes.count) return prediction ``` This function takes list tuples form `(feature_vector,label)` pairs alongside single feature vector representing unknown sample whose category needs estimating through comparison against known instances stored inside `data_points`.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值