poj1330-----------关于公共祖先的问题
关于公共祖先的问题分类:
这类问题有多种解法和类型,根据题目给出的信息去判断使用哪一种
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-----------关于公共祖先的问题的更多相关文章
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- LCA最近公共祖先(POJ1330)
题目链接:http://poj.org/problem?id=1330 解题报告: 先将一个子节点,深搜每一个根节点,并标记. 然后深索另一个子节点,当发现访问过了,就找到了最近的公共祖先. #inc ...
- hdu2586&&poj1330 求点间最短距&&最近公共祖先(在线&&离线处理):::可做模板
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj1330 lca 最近公共祖先问题学习笔记
首先推荐两个博客网址: http://dongxicheng.org/structure/lca-rmq/ http://scturtle.is-programmer.com/posts/30055. ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- LCA最近公共祖先 ST+RMQ在线算法
对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决. 这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...
- 【转】最近公共祖先(LCA)
基本概念 LCA:树上的最近公共祖先,对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. RMQ:区间最小值查询问题.对于长度为n的 ...
- 【并查集】【树】最近公共祖先LCA-Tarjan算法
最近公共祖先LCA 双链BT 如果每个结点都有一个指针指向它的父结点,于是我们可以从任何一个结点出发,得到一个到达树根结点的单向链表.因此这个问题转换为两个单向链表的第一个公共结点(先分别遍历两个链表 ...
- 洛谷P3379 【模板】最近公共祖先(LCA)
P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交 讨论 题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...
- Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】
一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...
随机推荐
- 2.2.5synchronized代码间的同步性
package com.cky.bean; /** * Created by chenkaiyang on 2017/12/6. */ public class ObjectService { pub ...
- AI模型训练/算法评估 测试员
- SSM_CRUD新手练习(2)配置文件
配置之前现需要引入依赖的jar包: *Spring *SpringMvc *Mybatis *数据库连接池,驱动包 *其他(jstl,Servlet ,Junit..) 1.poxm.xml < ...
- webView自适应及缩放
WebView wv=(WebView) findViewById(R.id.webView); wv.setVisibility(WebView.VISIBLE); WebSettings ws = ...
- hdu 5072 两两(不)互质个数逆向+容斥
http://acm.hdu.edu.cn/showproblem.php?pid=5072 求n个不同的数(<=1e5)中有多少组三元组(a, b, c)两两不互质或者两两互质. 逆向求解,把 ...
- POJ3046选蚂蚁创建集合_线性DP
POJ3046选蚂蚁创建集合 一个人的精力是有限的呢,如果一直做一件事迟早会疲惫,所以自己要把握好,不要一直埋头于一件事,否则效率低下还浪费时间 题目大意:一共有T(1,2...n为其种类)种蚂蚁,A ...
- 前端开发 - jQuery
本节内容 一.jQuery概述 二.选择器 三.操作DOM 四.修改DOM结构 五.事件 六.动画 七.AJAX(待续) 八.扩展(待续) 一.jQuery概述 jQuery 是一个 JavaScri ...
- 修改vsftpd的默认根目录
修改ftp的根目录只要修改/etc/vsftpd/vsftpd.conf文件即可: 加入如下几行: local_root=/var/www/html chroot_local_user=YES ano ...
- spring mvc 的请求流程
SpringMVC核心处理流程: 1.DispatcherServlet前端控制器接收发过来的请求,交给HandlerMapping处理器映射器 2.HandlerMapping处理器映射器,根据请求 ...
- python36--将数据保存为excel
#!/usr/bin/env python # -*- coding: utf-8 -*- import xlwt import os class ExcelHelper(object): @stat ...