关于公共祖先的问题分类:

这类问题有多种解法和类型,根据题目给出的信息去判断使用哪一种

1、给你树,只支持从父亲找儿子,不支持儿子找父亲,最后要求最近公共祖先,使用dfs或者分治

2、支持儿子找父亲,求最近公共祖先,逆序,从儿子找到底,然后比较(具体看本题代码,说不清)

3、不给树,求是否有共同祖先,并查集

4、还有离线、倍增等方法、

本题属于第二种:

1、从儿子找到底,把两条路径都记录下来

2、从后往前比对两条路径,从那里开始不一样,前一个就是最近的公共祖先

3、注意需要把自己也放进路径中,因为自己可能已经是别人的父亲了

4、使用map存放儿子-》父亲的关系比较好用,因为一个孩子只能有一个父亲。

代码如下:

import java.util.HashMap;
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int testCase = cin.nextInt();
for (int i = 0; i < testCase; i++){
int n = cin.nextInt();
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int j = 0; j < n-1; j++){
int parent = cin.nextInt();
int child = cin.nextInt();
map.put(child,parent);
}
int nodeOne = cin.nextInt();
int nodeTwo = cin.nextInt();
System.out.println(nca(nodeOne, nodeTwo, map, n));
}
} private static int nca(int nodeOne, int nodeTwo, HashMap<Integer,Integer> map, int maxLength){
int[] nodeOneArray = new int[maxLength];
nodeOneArray[0] = nodeOne;
int nodeOneIndex = 1;
while (map.containsKey(nodeOne)){
nodeOneArray[nodeOneIndex] = map.get(nodeOne);
nodeOne = nodeOneArray[nodeOneIndex];
nodeOneIndex++;
} int[] nodeTwoArray = new int[maxLength];
nodeTwoArray[0] = nodeTwo;
int nodeTwoIndex = 1;
while (map.containsKey(nodeTwo)){
nodeTwoArray[nodeTwoIndex] = map.get(nodeTwo);
nodeTwo = nodeTwoArray[nodeTwoIndex];
nodeTwoIndex++;
} nodeOneIndex--;
nodeTwoIndex--;
while (nodeOneArray[nodeOneIndex] == nodeTwoArray[nodeTwoIndex]){
nodeOneIndex--;
nodeTwoIndex--;
if (nodeOneIndex == -1){
return nodeOneArray[nodeOneIndex+1];
} else if (nodeTwoIndex == -1){
return nodeTwoArray[nodeTwoIndex+1];
}
}
return nodeOneArray[nodeOneIndex+1];
}
}

poj1330-----------关于公共祖先的问题的更多相关文章

  1. POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

    A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld &am ...

  2. LCA最近公共祖先(POJ1330)

    题目链接:http://poj.org/problem?id=1330 解题报告: 先将一个子节点,深搜每一个根节点,并标记. 然后深索另一个子节点,当发现访问过了,就找到了最近的公共祖先. #inc ...

  3. hdu2586&&poj1330 求点间最短距&&最近公共祖先(在线&&离线处理):::可做模板

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. poj1330 lca 最近公共祖先问题学习笔记

    首先推荐两个博客网址: http://dongxicheng.org/structure/lca-rmq/ http://scturtle.is-programmer.com/posts/30055. ...

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

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

  6. LCA最近公共祖先 ST+RMQ在线算法

    对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决.     这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...

  7. 【转】最近公共祖先(LCA)

    基本概念 LCA:树上的最近公共祖先,对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. RMQ:区间最小值查询问题.对于长度为n的 ...

  8. 【并查集】【树】最近公共祖先LCA-Tarjan算法

    最近公共祖先LCA 双链BT 如果每个结点都有一个指针指向它的父结点,于是我们可以从任何一个结点出发,得到一个到达树根结点的单向链表.因此这个问题转换为两个单向链表的第一个公共结点(先分别遍历两个链表 ...

  9. 洛谷P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交  讨论  题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...

  10. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...

随机推荐

  1. MFC模块状态(一)

    先看一个例子: 1.创建一个动态链接到MFC DLL的规则DLL,其内部包含一个对话框资源.指定该对话框ID如下:              #define IDD_DLL_DIALOG  2000 ...

  2. 1143 Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  3. hdu 3015

    这个题给你一堆树,每棵树的位置x和高度h都给你 f[i]代表这棵树的位置排名,s[i]代表这棵树的高度排名 问你任意两棵树的(f[i] - f[j])*min(s[i],s[j])和 (f[i]-f[ ...

  4. delphi IsIPAdress 非正则表达式验证IP的方法

    function IsIPAdress(const Value:String):Boolean; var n,x,i: Integer; Posi:Array[..]of Integer; Oktet ...

  5. C# Winform 换肤

    本来计划接着上篇 C# Winform模仿百度日历,发现一时半会写不完,只写了一小半还不全,暂且搁置下.现在计划下班后每天至少写一篇博客,未能完成的等周末(不加班都情况)补充完整. 本篇博客窗体换肤, ...

  6. 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑

    [源码下载] 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) InkCanv ...

  7. 面向对象总结、configparser配置文件模块、logging日志模块

    面向对象总结 # 学习态度# python基础 2个月# html css js jq 1个月 # 上课困 # 学习方法 :# 列出知识点# 例子 写了哪些 # 面向对象学了哪些块# 为什么要讲面向对 ...

  8. Quartz是一个任务调度

    这段时间做一个案子,用到每天定时处理事件,之前的解决思路是通过一个定时器轮询时间段,这样不能精准的在某个时间戳上执行动作.由于没有用过Quartz是一个任务调度,一直使用这个办法,今天通过同事提点,从 ...

  9. jmeter 中 浮点数计算精度问题

    jmeter 中 浮点数计算精度问题解决方法: 编写 beanshell 时使用 java.math.BigDecimal 方法构造,使用 BigDecimal 并且一定要用 String 来够造. ...

  10. CF1082解题报告

    CF1082A Vasya and Book 模拟一下即可 \(Code\ Below:\) #include <bits/stdc++.h> using namespace std; c ...