[路飞]_LeetCode_连通网络的操作次数

363 阅读2分钟

「这是我参与2022首次更文挑战的第17天,活动详情查看:2022首次更文挑战

题目

用以太网线缆将 n 台计算机连接成一个网络,计算机的编号从 0 到 n-1。线缆用 connections 表示,其中 connections[i] = [a, b] 连接了计算机 a 和 b。

网络中的任何一台计算机都可以通过网络直接或者间接访问同一个网络中其他任意一台计算机。

给你这个计算机网络的初始布线 connections,你可以拔开任意两台直连计算机之间的线缆,并用它连接一对未直连的计算机。请你计算并返回使所有计算机都连通所需的最少操作次数。如果不可能,则返回 -1 。 

示例 1:

image.png

输入:n = 4, connections = [[0,1],[0,2],[1,2]]
输出:1
解释:拔下计算机 12 之间的线缆,并将它插到计算机 13 上。

示例 2:

image.png

输入:n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
输出:2

示例 3:

输入:n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
输出:-1
解释:线缆数量不足。

示例 4:

输入:n = 5, connections = [[0,1],[0,2],[3,4],[2,3]]
输出:0

提示:

1 <= n <= 10^5
1 <= connections.length <= min(n*(n-1)/2, 10^5)
connections[i].length == 2
0 <= connections[i][0], connections[i][1] < n
connections[i][0] != connections[i][1]
没有重复的连接。
两台计算机不会通过多条线缆连接。

来源:力扣(LeetCode)leetcode-cn.com/problems/nu…

解题思路

电脑连接后会形成 n 个集合,要把所有集合连接在一起需要操作 n - 1 次。这种连通性的问题可以通过并查集来解决,电脑之间连接就是连通关系。

把连接插入并查集,得到集合数(根节点为自己的节点数),集合数减 1 就是需要操作的次数。

代码实现

var makeConnected = function (n, connections) {
    // n 台电脑需要 n - 1 个连接,如果连接不够则返回 -1
    if (connections.length < n - 1) return -1

    const u = new UnionSet(n)

    //将所有连接插入并查集
    for (const [a, b] of connections) {
        u.merge(a, b)
    }

    //统计集合的数量
    let cnt = 0
    for (let i = 0; i < n; i++) {
        if (u.get(i) === i) cnt++
    }

    // 连通 n 个集合需要操作 n - 1 次
    return cnt - 1
};

class UnionSet {
    constructor(n) {
        //初始化父节点数组,每个节点的父节点默认为自己
        this.pa = new Array(n + 1).fill(0).map((item, index) => index)

        //初始化每棵树的节点数
        this.size = new Array(n + 1).fill(1)
    }

    get(x) {
        //查找x的父节点,并且完成路径优化
        //优化后,x的父节点指向所在树的根节点
        return this.pa[x] = this.pa[x] === x ? x : this.get(this.pa[x])
    }

    merge(a, b) {
        //找到a的根节点
        const ra = this.get(a)
        //找到b的根节点
        const rb = this.get(b)

        //如果a和b在一个集合中则不需要合并
        if (ra === rb) return

        //把节点总数小的集合合并到节点总数多的集合里
        //更新节点总数多的集合为 a和b之和
        if (this.size[ra] < this.size[rb]) {
            this.pa[ra] = rb
            this.size[rb] += this.size[ra]
        } else {
            this.pa[rb] = ra
            this.size[ra] += this.size[rb]
        }
    }
}

如有错误欢迎指出,欢迎一起讨论!