Number of operations connected to the network java

Connect n computers to form a network with Ethernet cables, and the computer numbers are from 0 to n-1. Cables are represented by connections, where connections[i] = [a, b] connect computers a and b.

Any computer in the network can directly or indirectly access any other computer in the same network through the network.

To give you the initial wiring connections of this computer network, you can unplug the cable between any two directly connected computers and use it to connect a pair of computers that are not directly connected. Please calculate and return the minimum number of operations required to connect all computers. If it is not possible, -1 is returned.

Example 1:

Input: n = 4, connections = [[0,1],[0,2],[1,2]]
Output: 1
Explanation: Unplug the cable between computers 1 and 2, and plug it into the computer 1 and 3 on.
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: The number of cables is insufficient.
Example 4:

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

prompt:

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]
There are no duplicate connections.
Two computers will not be connected by multiple cables.

Source: LeetCode
Link: https://leetcode-cn.com/problems/number-of-operations-to-make-network-connected
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Idea: This question set template is enough. I wrote it myself, but I really didn’t understand the solution. I don’t think his size array can be used.

class Solution {
    
    
    public int makeConnected(int n, int[][] connections) {
    
    
        if (connections.length < n - 1) {
    
    
            return -1;
        }
        UnionFind uf = new UnionFind(n);
        for (int[] conn : connections) {
    
    
            uf.union(conn[0], conn[1]);
        }

        return uf.Count - 1;
    }
}

// 并查集模板
class UnionFind {
    
    
    int[] parent;
    //int[] size;
    int n;
    // 当前连通分量数目
    int Count;

    public UnionFind(int n) {
    
    
        this.n = 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];
    }
    
    public boolean union(int x, int y) {
    
    
        x = find(x);
        y = find(y);
        if (x == y) {
    
    
            return false;
        }
        //if (size[x] < size[y]) {
    
    
        //    int temp = x;
        //    x = y;
        //    y = temp;
        //}
        parent[x] = y;
        //size[x] += size[y];
        --Count;
        return true;
    }
    
    public boolean connected(int x, int y) {
    
    
        x = find(x);
        y = find(y);
        return x == y;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43824233/article/details/113050649