-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber_Of_Provinces.cpp
57 lines (48 loc) · 1.18 KB
/
Number_Of_Provinces.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
45
46
47
48
49
50
51
52
53
54
55
56
57
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution {
public:
void dfs(vector<vector<int>> &adj, vector<int> &vis, int curr)
{
vis[curr] = 1;
for(int i = 0; i < vis.size(); i++)
if(i != curr && adj[curr][i] && !vis[i])
dfs(adj, vis, i);
}
int numProvinces(vector<vector<int>> adj, int V) {
int cnt = 0;
vector<int> vis(V, 0);
for(int i = 0; i < V; i++)
{
cnt += !vis[i] ? dfs(adj, vis, i), 1 : 0;
}
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