力扣-图论-6【算法学习day.56】

前言

###我做这类文章一个重要的目的还是给正在学习的大家提供方向和记录学习过程(例如想要掌握基础用法,该刷哪些题?)我的解析也不会做的非常详细,只会提供思路和一些关键点,力扣上的大佬们的题解质量是非常非常高滴!!!


习题

1.统计完全连通分量的数量

题目链接: 2685. 统计完全连通分量的数量 - 力扣(LeetCode)

题面:

分析:我的做法是通过并查集来确定哪些节点是连在一起的,然后通过集合数组来判断连在一起的节点是否互相都存在路

代码:

java 复制代码
class Solution {
    int[] parent;
    int ans = 0;
    List<Integer>[] to;
    int n;
    public int countCompleteComponents(int n, int[][] edges) {
        this.n = n;
        parent = new int[n];
        for(int i = 0;i<n;i++){
            parent[i] = i;
        }
        to = new List[n];
        Arrays.setAll(to,_->new ArrayList<>());
        for(int[] arr:edges){
            int a = arr[0];
            int b = arr[1];
            union(a,b);
            to[a].add(b);
            to[b].add(a);
        }
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i<n;i++){
            if(map.getOrDefault(find(i),-1)==-1){
                map.put(find(i),1);
                recursion(find(i));
            }
        }
        return ans;
    }
    public void recursion(int parent){
        int[] arr = new int[n];
        int count = 0;
        for(int i = 0;i<n;i++){
            if(find(i)==parent){
                arr[count++] = i;
                // System.out.println(i);
            }
        }
        if(count==1){
            ans++;
            return;
        }
        for(int i = 0;i<count-1;i++){
            for(int j = i+1;j<count;j++){
                if(!isHave(arr[i],arr[j])){
                    return;
                }
            }
        }
        ans++;
    }
    public boolean isHave(int x,int y){
        for(int a:to[x]){
            if(a==y)return true;
        }
        return false;
    }

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

    public void union(int a,int b){
        int pa = find(a);
        int pb = find(b);
        if(pa!=pb){
            parent[pa]=pb;
        }
    }

    public boolean isInALine(int a,int b){
        return find(a)==find(b);

    }
}

2.有向无环图中的一个节点的所有祖先

题目链接: 2192. 有向无环图中一个节点的所有祖先 - 力扣(LeetCode)

题面:

分析:我的思路是利用集合数组在遍历edges存储表示节点i可被节点j到达,然后使用递归来找祖先节点,例如beto[1] = {2,3}表示节点1可以被节点2,3到达,那么我们把2,3存入Treeset集合,然后递归找2,3可以被哪些节点到达......,Treeset可以自动去重和排序,但是亲测复杂度很高

java 复制代码
class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer>[] beto;
    public List<List<Integer>> getAncestors(int n, int[][] edges) {
         beto = new List[n];
         Arrays.setAll(beto,_->new ArrayList<>());
         for(int[] arr:edges){
            beto[arr[1]].add(arr[0]);
         }
         TreeSet<Integer> set;
         for(int i = 0;i<n;i++){
            set =  new TreeSet<>();
            recursion(i,set);
            ans.add(new ArrayList<>(set));
         }
         return ans;
    }

    public void recursion(int x,TreeSet<Integer> set){
        for(int a:beto[x]){
            if(!set.contains(a)){
                 set.add(a);
                recursion(a,set);
            }
        }
    }

}

后言

上面是力扣图论专题,下一篇是其他的习题,希望有所帮助,一同进步,共勉!

相关推荐
龘龍龙3 分钟前
Python基础学习(十一)
python·学习·mysql
yuniko-n5 分钟前
【牛客面试 TOP 101】链表篇(一)
数据结构·算法·链表·面试·职场和发展
2501_9418053112 分钟前
从微服务网关到统一安全治理的互联网工程语法实践与多语言探索
前端·python·算法
源代码•宸13 分钟前
Leetcode—1161. 最大层内元素和【中等】
经验分享·算法·leetcode·golang
Chris_121916 分钟前
Halcon学习笔记-Day5
人工智能·笔记·python·学习·机器学习·halcon
CodeByV32 分钟前
【算法题】模拟
算法
悠哉悠哉愿意34 分钟前
【嵌入式学习笔记】AD/DA
笔记·单片机·嵌入式硬件·学习
s090713642 分钟前
FPGA加速:Harris角点检测全解析
图像处理·算法·fpga开发·角点检测
前端程序猿之路42 分钟前
30天大模型学习之Day 2:Prompt 工程基础系统
大数据·人工智能·学习·算法·语言模型·prompt·ai编程
硬件yun1 小时前
汽车CAN为何选用0.25W电阻?
学习