-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbfsgraph.cpp
More file actions
52 lines (52 loc) · 845 Bytes
/
bfsgraph.cpp
File metadata and controls
52 lines (52 loc) · 845 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
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void print(int c)
{
cout<<"connected components="<<c<<endl;
}
void bfs(vector<int>adj[],int src,int n)
{
vector<int> vis(n,false);
queue <int> qr;
int concom=0;
vis[src]=true;
qr.push(src);
while(!qr.empty())
{
int p=qr.front();
cout<<p<<" ";
qr.pop();
for(int i=0;i<adj[p].size();i++)
{
if(vis[adj[p][i]]==false)
{
concom+=1;
vis[adj[p][i]]=true;
qr.push(adj[p][i]);
}
}
}
print(concom);
}
int main()
{
int t=0;
cin>>t;
while(t--)
{
int vertex=0,edge=0;
cin>>vertex>>edge;
vector <int> adj[vertex+1];
for(int i=0;i<edge;i++)
{
int a=0,b=0;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int source=0;
cin>>source;
bfs(adj,source,vertex);
}
}