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]; } }
分类:
【101】算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2016-03-16 dfs和bfs的简单总结