Skip to content

Commit

Permalink
Create NumberOfProvinces.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored May 8, 2024
1 parent e8cc8a8 commit 5302162
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Graphs/NumberOfProvinces.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//User function Template for C++

class Solution {
public:
void dfs(int node,vector<vector<int>>&adjlist,vector<int>&visited){
visited[node]=1;
for(auto i: adjlist[node]){
if(!visited[i]){
dfs(i,adjlist,visited);
}
}
}
int numProvinces(vector<vector<int>> adj, int V) {
// code here
vector<vector<int>>adjlist(V);
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
if(adj[i][j]==1 && i!=j){
adjlist[i].push_back(j);
adjlist[j].push_back(i);
}
}
}
int count=0;
vector<int>visited(V,0);
for(int i=0;i<V;i++){
if(visited[i]==0){
count++;
dfs(i,adjlist,visited);
}
}
return count;
}
};

0 comments on commit 5302162

Please sign in to comment.