-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectedComponents.cpp
More file actions
41 lines (39 loc) · 870 Bytes
/
connectedComponents.cpp
File metadata and controls
41 lines (39 loc) · 870 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
#include <bits/stdc++.h>
using namespace std;
void dfs(int u, vector<bool> &vis, vector<vector<int> >g){
vis[u]=true;
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if(vis[v]) continue;
dfs(v,vis,g);
}
}
int main(){
IOS;
vector<vector<int> > g;
int V,E;
cin>>V>>E;
g.resize(V+10);
for(int i=0;i<E;i++){
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
cout<<endl;
for(int i=1;i<=V;i++){
for(int j=0;j<g[i].size();j++) cout<<g[i][j]<<" ";
cout<<endl;
}
cout<<endl;
vector<bool> vis(V+5, false);
int countComponents=0;
for(int i=1;i<=V;i++){
if(!vis[i]){
dfs(i,vis,g);
countComponents++;
}
}
cout<<"Number of connected components are: "<<countComponents;
return 0;
}