-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode : All connected components
More file actions
58 lines (57 loc) · 1.19 KB
/
Code : All connected components
File metadata and controls
58 lines (57 loc) · 1.19 KB
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
58
#include <bits/stdc++.h>
using namespace std;
void dfs(int **arr,int V,int sv,bool *visit,vector<int> *ans){
//cout<<sv<<" ";
ans->push_back(sv);
visit[sv]=true;
for(int i=0;i<V;i++){
if(i==sv){
continue;
}
if(arr[i][sv]==1 && !visit[i]){
dfs(arr,V,i,visit,ans);
}
}
}
void pdfs(int **arr,int V){
bool *visit=new bool[V];
for(int i=0;i<V;i++){
visit[i]=false;
}
int idx=0;
for(int i=0;i<V;i++){
if(!visit[i]){
vector<int> *ans=new vector<int> ();
dfs(arr,V,i,visit,ans);
sort(ans->begin(),ans->end());
for(int j=0;j<ans->size();j++){
cout<<ans->at(j)<<" ";
}
cout<<endl;
}
}
delete [] visit;
}
int main()
{
int V, E;
cin >> V >> E;
int **arr=new int*[V];
for(int i=0;i<V;i++){
arr[i]=new int[V];
for(int j=0;j<V;j++){
arr[i][j]=0;
}
}
for(int i=0;i<E;i++){
int f,s;cin>>f>>s;
arr[f][s]=1;
arr[s][f]=1;
}
pdfs(arr,V);
for(int i=0;i<V;i++){
delete [] arr[i];
}
delete [] arr;
return 0;
}