-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.cpp
53 lines (44 loc) · 837 Bytes
/
bfs.cpp
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
#include<bits/stdc++.h>
using namespace std;
int n = 200010;
queue<int> q;
vector< vector<int> > graph;
vector<bool>visit(n,false);
void bfs(int s)
{
q.push(s);
while(!q.empty())
{
int i;
int node = q.front();
printf("%d ",q.front());
q.pop();
visit[node] = true;
for( i=0; i<graph[node].size(); i++)
{
if(visit[graph[node][i]] == false)
{
visit[graph[node][i]] = true;
q.push(graph[node][i]);
}
}
}
}
int main()
{
int e,n,i,x,y;
scanf("%d %d",&n,&e);
graph.resize(n);
for(i=0;i<e;i++)
{
scanf("%d %d",&x,&y);
graph[x].push_back(y);
}
for(i=0;i<n;i++)
{
if(visit[i] == false)
{
bfs(i);
}
}
}