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 = ;
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=;i<=n;i++){
ances[i]=;
vis[i]=;
_rank[i] = ;
indegree[i]=;
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=;i<mp[u].size();i++)
{
Tarjan(mp[u][i]);
_union(u,mp[u][i]);
ances[_find(u)]=u;
}
vis[u]=;
if (A==u && vis[B]) printf("%d\n",ances[_find(B)]);
else if (B==u && vis[A]) printf("%d\n",ances[_find(A)]);
return ;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
int n;
scanf("%d",&n);
init(n);
int a,b;
for(int i=;i<n;i++){
scanf("%d%d",&a,&b);
mp[a].push_back(b);
indegree[b]++;
}
scanf("%d%d",&A,&B);
for(int i=;i<=n;i++){
if(indegree[i]==){
Tarjan(i);
break;
}
}
}
return ;
}

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

  1. POJ 1330 (LCA)

    http://poj.org/problem?id=1330 题意:给出一个图,求两个点的最近公共祖先. sl :水题,贴个模板试试代码.本来是再敲HDU4757的中间发现要用LCA,  操蛋只好用这 ...

  2. POJ 1330(LCA/倍增法模板)

    链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: #inc ...

  3. poj 1330(RMQ&LCA入门题)

    传送门:Problem 1330 https://www.cnblogs.com/violet-acmer/p/9686774.html 参考资料: http://dongxicheng.org/st ...

  4. Nearest Common Ancestors POJ - 1330 (LCA)

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

  5. POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)

    题意:给你一棵树,求两个点的最近公共祖先. 思路:因为只有一组询问,直接数组模拟好了. (写得比较乱) 原题请戳这里 #include <cstdio> #include <bits ...

  6. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  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 (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  9. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

随机推荐

  1. SRM12 T2夏令营(分治优化DP+主席树 (已更新NKlogN)/ 线段树优化DP)

     先写出朴素的DP方程f[i][j]=f[k][j-1]+h[k+1][i] {k<i}(h表示[k+1,j]有几个不同的数)  显然时间空间复杂度都无法承受   仔细想想可以发现对于一个点 i ...

  2. 仅此一文让你明白ASP.NET MVC 之View的显示

    有些人要问题,为什么我要学框架?这里我简单说一下,深入理解一个框架,给你带来最直接的好处: 使用框架时,遇到问题可以快速定位,并知道如何解决: 当框架中有些功能用着不爽时,你可以自由扩展,实现你想要的 ...

  3. train loss与test loss结果分析(接利用caffe的solverstate断点训练)

    train loss 不断下降,test loss不断下降,说明网络仍在学习; train loss 不断下降,test loss趋于不变,说明网络过拟合; train loss 趋于不变,test ...

  4. UVA 10201 DP

    Adventures in Moving - Part IV 题意: 汽车邮箱容量200升,最初有100升油,要求到达终点油箱中的油不少于100升的最小花费,不能到达终点输出Impossible. 汽 ...

  5. BI在连锁零售业应用

    BI案例:BI在连锁零售业应用(ZT) Posted on 2015-08-25 09:31 xuzhengzhu 阅读(42) 评论(0) 编辑 收藏 第一部分:连锁零售企业上BI的必要性. 目前国 ...

  6. [技巧篇]09.Struts2豁然开朗的一些配置[记得要看哟]

    这里留下一个重要的信息,关于部署描述符,关于struts2的核心配置文件,关于JSON插件的属性配置介绍,还有特别重要的JSON的注解 关于struts.xml的配置,这里学到了新的知识 使用插件方式 ...

  7. webpack插件url-loader使用规范

    其实说到性能优化,他的范围太广了,今天我们就只聊一聊通过webpack配置减少http请求数量这个点吧. 简单说下工作中遇到的问题吧,我们做的一个项目中首页用了十多张图片,每张图片都是一个静态资源,所 ...

  8. [POI2009]WIE-Hexer

    https://www.luogu.org/problem/show?pid=3489 题目描述 Byteasar has become a hexer - a conqueror of monste ...

  9. Beagleboneblack的MLO文件干了些啥

    Beagleboneblack在启动linux之前还有三个启动阶段: ROM code  -->  MLO  -->  u-boot --> kernel 先看看ROM code干了 ...

  10. 超酷算法-BK树

    前几天无意间遇到一个博客,觉得写得挺好的,自己之前的时候有个不好的习惯,那就是遇到了好资源第一反应就是收藏起来然后却很少再看!!这是坏习惯,要改!于是今天就开始通读了,读的第二篇是BK树.觉得有点意思 ...