Skip to content

Commit

Permalink
Added solution - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
sainikcodes24x7 committed Jul 2, 2023
1 parent e92eb15 commit 5c446b3
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Number of Provinces - GFG/number-of-provinces.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;


// } Driver Code Ends
//User function Template for C++

class Solution {
public:
void dfs(int idx,vector<vector<int>> &adj, vector<int>&vis, int V){
vis[idx]=1;

for(int i=0;i<V;i++){
if(vis[i]==0 and adj[idx][i]==1)
dfs(i,adj,vis,V);
}
}
int numProvinces(vector<vector<int>> adj, int V) {
// code here
vector<int>vis(V+1,0);
int cnt=0;
for(int i=0;i<V;i++){
if(!vis[i]){
cnt++;
dfs(i,adj,vis,V);
}

}
return cnt;
}
};

//{ Driver Code Starts.

int main() {
int t;
cin >> t;
while (t--) {
int V,x;
cin>>V;

vector<vector<int>> adj;

for(int i=0; i<V; i++)
{
vector<int> temp;
for(int j=0; j<V; j++)
{
cin>>x;
temp.push_back(x);
}
adj.push_back(temp);
}


Solution ob;
cout << ob.numProvinces(adj,V) << endl;
}
return 0;
}
// } Driver Code Ends

0 comments on commit 5c446b3

Please sign in to comment.