Skip to content

Commit

Permalink
Create 1319_Number of Operations to Make Network Connected.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
veryordinary11 authored Mar 24, 2023
1 parent 19be55f commit dbb6a58
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Graph/1319_Number of Operations to Make Network Connected.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
void dfs(vector<int> adj[], vector<bool> &visited, int src)
{
visited[src] = true;
for(int i : adj[src]){
if(!visited[i]){
dfs(adj, visited, i);
}
}
}
public:
int makeConnected(int n, vector<vector<int>>& arr) {
int len = arr.size();
if(len<n-1) return -1;
vector<int> adj[n];
for(auto v : arr)
{
adj[v[0]].push_back(v[1]);
adj[v[1]].push_back(v[0]);
}
vector<bool> visited(n, false);
int ans = 0;
for(int i=0; i<n; i++)
if(!visited[i])
{
dfs(adj, visited, i);
ans++;
}
return ans - 1;
}
};


0 comments on commit dbb6a58

Please sign in to comment.