forked from thegauravparmar/CodingNinjasRound2Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountry Groups.cpp
46 lines (41 loc) · 898 Bytes
/
Country Groups.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
#include<bits/stdc++.h>
using namespace std;
void udfs(int f, vector<int> arr[], vector<bool> &visited)
{
visited[f] = true;
for (int i=0; i<arr[f].size(); i++)
if (visited[arr[f][i]] == false)
udfs(arr[f][i], arr, visited);
}
int TreeCount(vector<int> arr[], int n)
{
vector<bool> visited(n, false);
int count = 0;
for (int u=0; u<n; u++)
{
if (visited[u] == false)
{
udfs(u, arr, visited);
count++;
}
}
return count;
}
void edgeadd(vector<int> arr[], int l, int m)
{
arr[l].push_back(m);
arr[m].push_back(l);
}
int main()
{
int n,t; cin>>n>>t;
int x,y;
vector<int> arr[n];
while(t--)
{
cin>>x>>y;
edgeadd(arr,x,y);
}
cout << TreeCount(arr, n);
return 0;
}