1319. Number of Operations to Make Network Connected Medium

@problem@discussion
#Depth-First Search#Breadth-First Search#Union Find#Graph



1/**
2 * [1319] Number of Operations to Make Network Connected
3 *
4 * There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
5 * You are 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.
6 * Return the minimum number of times you need to do this in order to make all the computers connected. If it is not possible, return -1.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2020/01/02/sample_1_1677.png" style="width: 500px; height: 148px;" />
10 * Input: n = 4, connections = [[0,1],[0,2],[1,2]]
11 * Output: 1
12 * Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.
13 * 
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/01/02/sample_2_1677.png" style="width: 500px; height: 129px;" />
16 * Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
17 * Output: 2
18 * 
19 * Example 3:
20 * 
21 * Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
22 * Output: -1
23 * Explanation: There are not enough cables.
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	1 <= n <= 10^5
29 * 	1 <= connections.length <= min(n * (n - 1) / 2, 10^5)
30 * 	connections[i].length == 2
31 * 	0 <= ai, bi < n
32 * 	ai != bi
33 * 	There are no repeated connections.
34 * 	No two computers are connected by more than one cable.
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/number-of-operations-to-make-network-connected/
40// discuss: https://leetcode.com/problems/number-of-operations-to-make-network-connected/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn make_connected(n: i32, connections: Vec<Vec<i32>>) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_1319() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.