力扣-图论-3【算法学习day.53】

前言

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


1.统计无向图中无法互相到达点对数

题目链接: 2316. 统计无向图中无法互相到达点对数 - 力扣(LeetCode)

题面:

代码:

java 复制代码
class Solution {
    public long countPairs(int n, int[][] edges) {
        UF uf = new UF(n);
        for (int[] edge : edges) {
            uf.union(edge[0], edge[1]);
        }
        int[] size = uf.size();
        // 记录所有分支的大小
        List<Integer> list = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            // 找到节点 i 的根节点
            // 注意:只有每个连通分量的根节点的 size[] 才可以代表该连通分量中的节点数
            int p = uf.find(i);
            // 已经加入 list 的节点直接跳过
            if (!set.contains(p)) list.add(size[p]);
            set.add(p);
        }
        long ans = 0;
        // 计算结果
        for (int sz : list) ans += (long) sz * (n - sz);
        // 注意 ➗ 2
        return ans / 2;
    }
}
/* ------------ 并查集模版 ------------ */
class UF {
    private int count;
    private int[] parent;
    private int[] size;
    public UF(int n) {
        this.count = n;
        parent = new int[n];
        size = new int[n];
        for (int i = 0; i < n; i++) {
            parent[i] = i;
            size[i] = 1;
        }
    }
    public void union(int p, int q) {
        int rootP = find(p);
        int rootQ = find(q);
        if (rootP == rootQ) return ;
        // 平衡性优化
        if (size[rootP] < size[rootQ]) {
            parent[rootP] = rootQ;
            size[rootQ] += size[rootP];
        } else {
            parent[rootQ] = rootP;
            size[rootP] += size[rootQ];
        }
        this.count--;
    }
    public boolean connected(int p, int q) {
        int rootP = find(p);
        int rootQ = find(q);
        return rootP == rootQ;
    }
    public int count() {
        return this.count;
    }
    // 增加了一个函数
    // 返回 size[]
    public int[] size() {
        return this.size;
    }
    public int find(int x) {
        // 路径压缩
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }
}

后言

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

相关推荐
开源Z11 分钟前
LeetCode 42 · 接雨水:从暴力到双指针的三步优化
算法·leetcode
旖-旎20 分钟前
《LeetCode 695 岛屿的最大面积 FloodFill DFS 解法》
c++·算法·力扣·深度优先遍历·floodfill
MartinYeung524 分钟前
[论文学习]重新思考大型语言模型忘却目标:梯度视角与超越
人工智能·学习·语言模型
syagain_zsx1 小时前
STL 之 vector 讲练结合
c++·算法
十月的皮皮1 小时前
C语言学习笔记20260615-有序升序序列合并
c语言·笔记·学习
JAVA面经实录9171 小时前
前端系统化学习计划表(含完整知识思维导图)
前端·学习
worilb2 小时前
Spring Cloud 学习与实践(9):Gateway + JWT 统一鉴权
学习·spring cloud·gateway
MartinYeung52 小时前
[论文学习]DP2Unlearning:高效且具保证的大型语言模型遗忘框架(基于差分隐私的 LLM Unlearning 方法)
学习·算法·语言模型
Tian_Hang2 小时前
C++原型模式(Protype)
开发语言·c++·算法