Fork me on GitHub

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];
    }
}
复制代码
posted @   LinkinStar  阅读(299)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2016-03-16 dfs和bfs的简单总结
点击右上角即可分享
微信分享提示