-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbfs.cpp
More file actions
68 lines (67 loc) · 1.34 KB
/
Copy pathbfs.cpp
File metadata and controls
68 lines (67 loc) · 1.34 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
#include <bits/stdc++.h>
#define ll long long int
#define N 1000
#define M 1000000007
#define f(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define po pop_back
#define pb push_back
#define lb lower_bound
#define fi first
#define se second
#define mp make_pair
#define bs binary_search
#define debug(x) cout << #x << " = " << x << endl
#define ub upper_bound
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define all(x) x.begin(), x.end()
#define PI 3.14159265
#define cot cout.tie(0)
using namespace std;
/**
* @author :: Rishabh
*
*/
void bfs(vector<int> g[], int s) {
queue<int>q;
map<int,bool>m;
q.push(s);
m[s]=true;
while(!q.empty())
{
int t=q.front();
q.pop();
cout<<t<<" ";
for(auto x:g[t])
{
if(m[x]==false)
{
q.push(x);
m[x]=true;
}
}
}
}
int main()
{
ibs;
cti;
int t;
cin>>t;
while(t--)
{
int v,e;
cin>>v>>e;
vector<int>adj[v];
for(int i=0;i<e;i++)
{
int v1,v2;
cin>>v1>>v2;
adj[v1].pb(v2);
adj[v2].pb(v1);
}
bfs(adj,0);
}
return 0;
}