forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConecComp.cpp
More file actions
55 lines (44 loc) · 928 Bytes
/
ConecComp.cpp
File metadata and controls
55 lines (44 loc) · 928 Bytes
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
// Counting the no of connected componnents
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
vector<int> adj[100001];
int vis[100001];
void dfs(int val)
{
vis[val] = 1;
for (int child : adj[val])
{
if (!vis[child])
dfs(child);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int i, a, b, n, m;
cin >> n; //no of nodes
cin >> m; //no of edges
for (i = 0; i < m; i++)
{
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int cv = 0;
for (i = 1; i <= n; i++)
{
if (vis[i] == 0) //checking whether the node is visited or not
{
dfs(i); //calling the dfs function
cv++;
}
}
cout << cv;
}