-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAllConnectedComponents.cpp
More file actions
81 lines (74 loc) · 1.42 KB
/
AllConnectedComponents.cpp
File metadata and controls
81 lines (74 loc) · 1.42 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
void f(int **matrix,int V,int start,bool* visited,vector<int>v)
{
visited[start]=true;
queue<int>q;
q.push(start);
while(!q.empty())
{
int current=q.front();
v.push_back(q.front());
q.pop();
for(int i=0;i<V;i++)
{
if(matrix[i][current]==1 &&!visited[i])
{
q.push(i);
visited[i]=true;
}
}
}
sort(v.begin(),v.end());
for(int i=0;i<=v.size()-1;i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
bool checkvisited(int **matrix,int V,int start)
{
vector<int>v;
bool *visited=new bool[V];
for(int i=0;i<V;i++)
{
visited[i]=false;
}
for(int i=0;i<V;i++)
{
if(visited[i]==false)
{
f(matrix,V,i,visited,v);
}
}
}
int main()
{
int V, E, tempX, tempY;
cin >> V >> E;
/*
Write Your Code Here
Complete the Rest of the Program
You have to Print the output yourself
*/
int **matrix=new int*[V];
for(int i=0;i<V;i++)
{
matrix[i]=new int[V];
for(int j=0;j<V;j++)
{
matrix[i][j]=0;
}
}
while(E--)
{
int s,f;
cin>>s>>f;
matrix[s][f]=matrix[f][s]=1;
}
checkvisited(matrix,V,0);
return 0;
}