-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy path10_making_wired_connections.cpp
44 lines (33 loc) · 1.11 KB
/
10_making_wired_connections.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
/*
link: https://leetcode.com/problems/number-of-operations-to-make-network-connected/
just count the disconnected graph
*/
// ----------------------------------------------------------------------------------------------------------------------- //
void dfs(int curr, vector<int> v[], vector<int>& vis) {
vis[curr] = 1;
for (auto i : v[curr]) {
if (!vis[i]) {
dfs(i, v, vis);
}
}
}
int makeConnected(int n, vector<vector<int>>& connections) {
// if cables(edges) is less than vertex-1 means all computer(vertex) won't connect
if (connections.size() < n - 1) return -1;
vector<int> vis(n, 0);
vector<int> v[n];
for (int i = 0;i < connections.size();i++) {
v[connections[i][0]].push_back(connections[i][1]);
v[connections[i][1]].push_back(connections[i][0]);
}
int disconnected = 0;
for (int i = 0;i < n;i++) {
if (!vis[i]) {
disconnected++;
dfs(i, v, vis);
}
}
// dis -1 as initial network wont count
// the one out of which we will provide cable
return disconnected - 1;
}