美文网首页
LCA Tarjan Java实现

LCA Tarjan Java实现

作者: kaiker | 来源:发表于2023-03-27 10:06 被阅读0次

https://blog.csdn.net/qq_36799943/article/details/78250697 这个图里的子树讲的比较好
https://www.cnblogs.com/zl1991/p/13094997.html 代码的思路借鉴了一些这篇文章

dfs序的主要作用就是将一个子树变成序列上的一个连续区间。
https://www.zhihu.com/question/46440863/answer/2265938163?utm_id=0

import java.util.*;

class TreeNode {
    public int val;
    public List<TreeNode> children;

    public TreeNode(int val) {
        this.val = val;
        children = new ArrayList<>();
    }
}

class TarjanLCA {
    int[] parent; // 并查集的代表元数组
    boolean[] visited; // 是否已访问
    int ans; // 存储每个单个的LCA
    int[] query; // 存储每个查询的参数

    public int findLCA(TreeNode root, int[] query) {
        // 初始化参数
        this.parent = new int[100];
        this.visited = new boolean[100];
        this.query = query;
        for (int i = 0; i < parent.length; i++) {
            parent[i] = i;
        }
        // 预处理节点深度并建图
        dfs(root);
        return ans;
    }

    private void dfs(TreeNode u) {
        for (TreeNode child : u.children) {
            if (!visited[child.val]) {
                dfs(child);
                union(u.val, child.val);
                visited[child.val] = true;
            }
        }
        if (u.val == query[0] && visited[query[1]]) {
            ans = find(query[1]);
            return;
        } else if (u.val == query[1] && visited[query[0]]) {
            ans = find(query[0]);
            return;
        }
    }

    private int find(int x) {
        if (parent[x] == x) {
            return x;
        }
        return find(parent[x]);
    }

    // 默认父节点为x
    private void union(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) {
            parent[rootY] = rootX;
        }
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.children.add(new TreeNode(2));
        root.children.add(new TreeNode(3));
        root.children.get(0).children.add(new TreeNode(4));
        root.children.get(0).children.add(new TreeNode(5));
        root.children.get(1).children.add(new TreeNode(6));
        root.children.get(1).children.add(new TreeNode(8));
        root.children.get(1).children.get(0).children.add(new TreeNode(7));
        root.children.get(1).children.get(1).children.add(new TreeNode(9));

        int[] query = {7,9};
        int ans = new TarjanLCA().findLCA(root, query);
        System.out.println(ans);
    }
}

相关文章

  • 刷题计划

    并查集 题目 图论 题目 树 题目 资源参考 倍增LCA LCA和RMQ Tarjan LCA 倍增 题目 DP 题目

  • luogu P3379 【模板】最近公共祖先(LCA)

    lca最近公共祖先,是指两个点最近的祖先节点;求lca我知道的有三种倍增,st表,tarjan,我要介绍的是倍增,...

  • tarjan-LCA最近公共祖先离线算法

    在一棵树上查询任意两个点的最近公共祖先,或求最短距离的离线算法tarjan,基于dfs遍历和并查集,在查询时倍增直...

  • 最近公共祖先(LCA-tarjan/RMQ/倍增)

    在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先...

  • tarjan算法动态演示

    java swing写的tarjan算法[https://zhuanlan.zhihu.com/p/1019233...

  • 并查集

    刚写到LCA的tarjan算法,合并需要用到并查集,那么这里就把普通并查集进行贴下版吧。并查集是一种很优美的数据结...

  • 2018-12-09

    lCA

  • tarjan

    tarjan缩点的运用,寻找一个较小的点集使得从这些点出发能够到达任意不在点集中的点,若有多个点,输出这些集合升序...

  • tarjan

    tarjan:寻找出度为0的强连通分量,并求出该强连通分量中有多少个点。 sig表示的是强连通分量的个数其中col...

  • tarjan

    tarjan寻找出度为0的强连通分量,从小到大输出此强连通分量中的点 poj 2553 The Bottom of...

网友评论

      本文标题:LCA Tarjan Java实现

      本文链接:https://www.haomeiwen.com/subject/cwzordtx.html