-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathNumber of Operations to Make Network Connected.cpp
56 lines (41 loc) · 1.54 KB
/
Number of Operations to Make Network Connected.cpp
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
/*
Solution by Rahul Surana
***********************************************************
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.
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.
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.
***********************************************************
*/
#include<bits/stdc++.h>
class Solution {
public:
vector<vector<int>> adj;
vector<bool> v;
void cc(int i){
if(v[i]) return;
v[i] = true;
for(auto z: adj[i]){
cc(z);
}
}
int makeConnected(int n, vector<vector<int>>& connections) {
if(connections.size()<n-1) return -1;
adj.resize(n,vector<int>());
v.resize(n,false);
for(auto z: connections){
adj[z[0]].push_back(z[1]);
adj[z[1]].push_back(z[0]);
}
int ans = 0;
for(int i = 0; i < n; i++){
if(!v[i]){
cc(i);
ans++;
}
}
return ans-1;
}
};