其他分享
首页 > 其他分享> > 【LeetCode】1319.连通网络的操作次数(Number of Operations to Make Network Connected)

【LeetCode】1319.连通网络的操作次数(Number of Operations to Make Network Connected)

作者:互联网

目录

题目描述

There are n computers numbered from 0 to n-1 connected by ethernet cables connections forming a network where connections[i] = [a, b] represents a connection between computers a and b. Any computer can reach any other computer directly or indirectly through the network.

Given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected. Return the minimum number of times you need to do this in order to make all the computers connected. If it’s not possible, return -1.

Example 1:

Input:n = 4, connections = [[0,1],[0,2],[1,2]]
Output:1
Explanation:Remove cable between computer 1 and 2 and place between computers 1 and 3.

Example 2:

Input:n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
Output:2

Example 3:

Input:n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
Output:-1
Explanation:There are not enough cables.

Example 4:

Input:n = 5, connections = [[0,1],[0,2],[3,4],[2,3]]
Output:0

Constraints:

题目来源:https://leetcode-cn.com/problems/number-of-operations-to-make-network-connected/

题目大意

目前一共有 n n n台电脑,分别编号为 0 , 1 , ⋯   , n − 1 0,1,\cdots,n - 1 0,1,⋯,n−1,connections数组描述了它们之间的网线连接情况,connections[i] = [a, b]表明编号为 a a a、 b b b的电脑之间有一条网络。为了保证这 n n n台电脑之间是互连的,可以拆开两台电脑之间的网络用于连接其它未连接的电脑,题目问最少需操作多少次,并返回该值;而如果无法使得这 n n n台电脑互连,则返回-1。

解题方法

方法一:并查集

思路
首先要明确的是,要使 n n n台电脑之间是互连的,至少需要 n − 1 n - 1 n−1条网线。因此,如果输入数组connections中的网线数量少于 n − 1 n - 1 n−1,则说明这些电脑不可能全部互连,应该直接返回-1。除此之外,这道题的思路与547.省份数量几乎是一摸一样的,本质上是让我们求出图中连通块的数量 k k k,由于每个连通块中的电脑是互连的,那么只需让这 k k k个连通块互连就能让所有电脑都互连了,因此最少操作次数就为 k − 1 k - 1 k−1。
代码

class Solution {
    int[] p;

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

    public void union(int x, int y){
        p[find(x)] = find(y);
    }

    public int makeConnected(int n, int[][] connections) {
        int cablesNum = connections.length;
        if(cablesNum < n - 1) return -1;
        p = new int[n];
        for(int i = 0; i < n ; i ++) p[i] = i;
        for(int i = 0; i < cablesNum; i ++) union(connections[i][0], connections[i][1]);
        
        int parts = 0;
        for(int i = 0; i < n; i ++){
            if(p[i] == i) parts ++;
        }

        return parts - 1;
    }
}

复杂度分析

标签:Operations,Network,int,Make,computers,电脑,connections,复杂度,互连
来源: https://blog.csdn.net/m0_54964586/article/details/113574007