• 力扣 1319. 连通网络的操作次数


    题目

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

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

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

    示例

    在这里插入图片描述
    输入:n = 4, connections = [[0,1],[0,2],[1,2]]
    输出:1
    解释:拔下计算机 1 和 2 之间的线缆,并将它插到计算机 1 和 3 上。

    在这里插入图片描述
    输入:n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
    输出:2

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

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

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/number-of-operations-to-make-network-connected
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    方法1:并查集在这里插入图片描述

    Java实现

    平衡性优化:size数组。将小的树接到大的树下面
    路径压缩:find()

    class Solution {
        public int makeConnected(int n, int[][] connections) {
            if (connections.length < n - 1) return -1;
            UF uf = new UF(n);
    
            for (int[] con : connections) {
                uf.unite(con[0], con[1]);
            }
            return uf.count - 1;
        }
    
        class UF {
            private int count; //连通分量个数
            int n; //图中节点个数
            private int[] parent; //存储每个节点的父节点
    
            private int[] size; //每个节点的个数,确保小树接到大树下
    
            public UF(int n) {
                this.count = n;
                this.parent = new int[n];
                this.size = new int[n];
                Arrays.fill(size, 1);
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                }
            }
    
            //找每个节点的根节点
            public int find(int x) {
                if (parent[x] != x) {
                    parent[x] = find(parent[x]);
                }
                return parent[x];
            }
    
            //将节点x和节点y连通
            public boolean unite(int x, int y) {
                x = find(x);
                y = find(y);
    
                if (x == y) return false;
    
                if (size[x] < size[y]) {
                    int tmp = x;
                    x = y;
                    y = tmp;
                }
                parent[y] = x;
                size[x] += size[y];
                count--;
                return true;
            }
    
            public boolean connected(int x, int y) {
                x = find(x);
                y = find(y);
                return x == y;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    在这里插入图片描述

  • 相关阅读:
    自定义注解实现日志打印时屏蔽特定字段不打印
    Startdrive中上传参数设置的具体方法和注意事项
    蓝牙耳机国产哪款好?国产高性价比蓝牙耳机盘点
    antd中 a-table表格组件跨页勾选,数据回显勾选的问题
    C#《原CSharp》第三回 万文疑谋生思绪 璃月港口见清玉
    CMU 15-445 Project 0 实现字典树
    选择共享wifi项目哪个公司好?!
    教程图文详解 - 广域网通信(第三章)
    C# ToString
    Servlet生命周期-9
  • 原文地址:https://blog.csdn.net/qq_42467009/article/details/125416200