poj 1330(初探LCA)

Nearest Common Ancestors

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23795   Accepted: 12386

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

下面这段模板代码引自某位大神的博客 http://www.cppblog.com/menjitianya/archive/2015/12/10/212447.html

void LCA_Tarjan(int u) { 

        make_set(u);                               // 注释1

        ancestor[ find(u) ] = u;                   // 注释2

        for(int i = 0; i < edge[u].size(); i++) {  // 注释3

            int v = edge[u][i].v; 

            LCA_Tarjan(v);                         // 注释4

            merge(u, v);                           // 注释5

            ancestor[ find(u) ] = u;               // 注释6

        }

        colors[u] = 1;                             // 注释7

        for(int i = 0; i < lcaInfo[u].size(); i++) {

            int v = lcaInfo[u][i].v;

            if(colors[v]) {                        // 注释8

                lcaDataAns[ lcaInfo[u][i].idx ].lca = ancestor[ find(v) ];

            }

        }

    }

注释1:创建一个集合,集合中只有一个元素u,即{ u }

注释2:因为u所在集合只有一个元素,所以也可以写成ancestor[u] = u

注释3:edge[u][0...size-1]存储的是u的直接子结点

注释4:递归计算u的所有直接子结点v

注释5:回溯的时候进行集合合并,将以v为根的子树和u所在集合进行合并

注释6:对合并完的集合设置集合对应子树的根结点,find(u)为该集合的代表元

注释7:u为根的子树访问完毕,设置结点颜色

注释8:枚举所有和u相关的询问(u, v),如果以v为根的子树已经访问过,那么ancestor[find(v)]肯定已经计算出来,并且必定是u和v的LCA

对于注释8的for循环,这个题只查询两个点,没必要用另外一个邻接表了

//题意:t组测试用例,输入n,接下来输入n-1条边 组成一棵树,第n组数据代表询问a b之间最近公共祖先
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const int MAX = 10005;
vector<int> mp[MAX];
int father[MAX];
int indegree[MAX];  ///入度为0的就是根节点
int _rank[MAX];  ///利用启发式合并防止树成链
int vis[MAX];
int ances[MAX];
int A,B;
void init(int n){
    for(int i=1;i<=n;i++){
        ances[i]=0;
        vis[i]=0;
        _rank[i] = 0;
        indegree[i]=0;
        father[i] = i;
        mp[i].clear();
    }
}
int _find(int x){
    if(x==father[x]) return x;
    return father[x] = _find(father[x]);
}
void _union(int a,int b){
    int x = _find(a);
    int y = _find(b);
    father[x]=y;
}
 int Tarjan(int u)
 {
     for (int i=0;i<mp[u].size();i++)
     {
         Tarjan(mp[u][i]);
         _union(u,mp[u][i]);
         ances[_find(u)]=u;
     }
     vis[u]=1;
    if (A==u && vis[B]) printf("%d\n",ances[_find(B)]);
     else if (B==u && vis[A]) printf("%d\n",ances[_find(A)]);
     return 0;
 }
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        int n;
        scanf("%d",&n);
        init(n);
        int a,b;
        for(int i=1;i<n;i++){
            scanf("%d%d",&a,&b);
            mp[a].push_back(b);
            indegree[b]++;
        }
        scanf("%d%d",&A,&B);
        for(int i=1;i<=n;i++){
            if(indegree[i]==0){
               Tarjan(i);
                break;
            }
        }
    }
    return 0;
}
时间: 2024-08-25 06:46:37

poj 1330(初探LCA)的相关文章

POJ 1330 (LCA)

http://poj.org/problem?id=1330 题意:给出一个图,求两个点的最近公共祖先. sl :水题,贴个模板试试代码.本来是再敲HDU4757的中间发现要用LCA,  操蛋只好用这个题目试试自己写的对不对. 下面是个倍增的写法,挺实用的. 好了,继续... 1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #inclu

POJ 1330(LCA模板)

链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 #include<set> 6 #include<string> 7 #incl

POJ 1330 Nearest Common Ancestors 倍增算法的LCA

POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节点的第2j个父亲是多少   这个代码不是我的,转自 邝斌博客 1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-9-5 9:45:17 4 File Name :F

POJ 1330 Nearest Common Ancestors (在线LCA转RMQ)

题目地址:POJ 1330 在线LCA转RMQ第一发.所谓在线LCA,就是先DFS一次,求出遍历路径和各个点深度,那么求最近公共祖先的时候就可以转化成求从u到v经过的点中深度最小的那个. 纯模板题. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h&g

LCA 算法学习 (最近公共祖先)poj 1330

poj1330 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,处理技巧就是在回溯到结点u的时候,u的子树已经遍历,这时候才把u结点放入合并集合中,这样u结点和所有u的子树中的结点的最近公共祖先就是u了,u和还未遍历的所有u的兄弟结点及子树中的最近公共祖先就是u的父亲结点.这样我们在对树深度遍历的时候就很自然的将树中的结点分成若干的集合,两个集合中的所属不同集合的任意一对顶点的公共祖先都是相同的,也就是说这两个集合的最近公共祖先只有一个.时间复杂度为O(n+q

POJ 1330 Nearest Common Ancestors LCA题解

本题是一个多叉树,然后求两点的最近公共单亲节点. 就是典型的LCA问题.这是一个很多解法的,而且被研究的很透彻的问题. 原始的解法:从根节点往下搜索,若果搜索到两个节点分别在一个节点的两边,那么这个点就是最近公共单亲节点了. Trajan离线算法:首次找到两个节点的时候,如果记录了他们的最低单亲节点,那么答案就是这个最低的单亲节点了. 问题是如何有效记录这个最低单亲节点,并有效根据遍历的情况更新,这就是利用Union Find(并查集)记录已经找到的节点,并及时更新最新访问的节点的当前最低单亲节

POJ 1330 裸的LCA

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17720   Accepted: 9393 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each

POJ 1330 Nearest Common Ancestors LCA(在线RMQ,离线Tarjan)

链接:http://poj.org/problem?id=1330 题意:只看题目就知道题目是什么意思了,最近公共祖先,求在一棵树上两个节点的最近公共祖先. 思路:求最近公共祖先有两种算法,在线和离线,在线方法是用RMQ求LCA,一句话总结就是在从DFS时,从第一个点到第二个点的最短路径中深度最浅的点就是公共祖先,用RMQ处理,一般问题的最优解决方式的复杂度是O(NlogN)的预处理+N*O(1)的查询.离线方法是Tarjan算法,将所有询问的两个点都记录下来,在DFS过程中不断将每个点自身作为

POJ 1330 LCA裸题~

POJ 1330 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 anc