力扣-图论-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];
    }
}

后言

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

相关推荐
我叫洋洋8 分钟前
C ++ [ hello world ]
c语言·c++·算法
圣光SG1 小时前
Servlet学习笔记
笔记·学习·servlet
奋发向前wcx1 小时前
y1,y2总复习笔记5 2026.7.19
数据结构·笔记·算法
@Mike@3 小时前
02-数据库学习笔记(SQL引擎)
数据库·笔记·学习
六点_dn4 小时前
RabbitMQ学习笔记-定义与作用
笔记·学习·rabbitmq
我的xiaodoujiao4 小时前
快速学习Python基础知识详细图文教程9--函数进阶
开发语言·python·学习·测试工具
战族狼魂5 小时前
高频面试题精选:分治与AI Agent架构
人工智能·算法·大模型·大语言模型
李剑一5 小时前
你用过网易的将军令嘛?它底层实现账号保护的原理相当简单粗暴
算法
Scott9999HH5 小时前
告别流量波动玄学!从法拉第电磁定律到底层 C/C++ 流量累积算法,深度解密工业级电磁流量计选型与开发
c语言·c++·算法
中达瑞和-高光谱·多光谱5 小时前
1nm到8nm光谱分辨率,你的应用该选哪款光谱相机?
算法·高光谱·高光谱相机