-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBFS_STL.cpp
More file actions
51 lines (41 loc) · 832 Bytes
/
BFS_STL.cpp
File metadata and controls
51 lines (41 loc) · 832 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
#include<bits/stdc++.h>
using namespace std;
void bfs(int s, vector<int> adj[], bool vis[], int N)
{
queue<int> q;
q.push(s);
while(!q.empty()){
int node = q.front();
q.pop();
if(!vis[node]){
cout<<node<<" ";
vis[node] = true;
}
for(auto it: adj[node]){
if(!vis[it]){
q.push(it);
}
}
}
}
int main()
{
freopen("input.txt", "r", stdin);
int T;
cin>>T;
while(T--)
{
int N, E;
cin>>N>>E;
vector<int> adj[N];
bool vis[N] = {false};
for(int i=0;i<E;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
}
bfs(0, adj, vis, N);
cout<<endl;
}
}