-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmakeNetworkConnected.cpp
More file actions
46 lines (30 loc) · 1.83 KB
/
makeNetworkConnected.cpp
File metadata and controls
46 lines (30 loc) · 1.83 KB
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
class Solution {
private:
void dfs(vector<int> g[], int x, vector<bool>& v){
v[x] = true; // We are on node 'x', so we set 'visited' as true.
for(int i = 0; i < g[x].size(); i++){ // Iterate through its neighbours.
if(v[g[x][i]] == false) // If neighbour is not visited, we apply dfs on it
dfs(g, g[x][i], v); // and visit it.
}
}
public:
int makeConnected(int n, vector<vector<int>>& conn) {
if(conn.size() < n-1) return -1; // If edges < n-1 that is, even a tree cannot be
// formed, it is impossible to connect the system.
vector<int> g[n]; // Defining an adjacency list.
for(int i = 0; i < conn.size(); i++){ // Iterating through connections
g[conn[i][0]].push_back(conn[i][1]); // Creating adjacency list, that is, an
g[conn[i][1]].push_back(conn[i][0]); // undirected graph.
}
vector<bool> visited(n, false); // Visited array to store if the node is already
// visited, initially set to false.
int comp = 0; // Stores number of distinct connected components.
for(int i = 0; i < n; i++){ // Traversing graph, i.e. adjacency list.
if(visited[i] == false){ // If not visited yet, we apply dfs on that node to
dfs(g, i, visited); // traverse the connected component. The number of times
comp++; // we call dfs is the number of distinct connected
} // components we have in our graph.
}
return comp-1; // n distinct connected components can be joined using n-1 cables.
}
};