-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbfs.cpp
More file actions
60 lines (47 loc) · 881 Bytes
/
Copy pathbfs.cpp
File metadata and controls
60 lines (47 loc) · 881 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
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
vector<bool> v;
vector<vector<int>> g;
void func(int a, int b)
{
g[a].push_back(b);
}
void bfs(int u)
{
queue<int> q;
q.push(u);
v[u] = true;
while (!q.empty())
{
int f = q.front();
q.pop();
cout << f << " ";
for (auto i = g[f].begin(); i != g[f].end(); i++)
{
if (!v[*i])
{
q.push(*i);
v[*i] = true;
}
}
}
}
int main()
{
int n, e;
cin >> n >> e;
v.assign(n, false);
g.assign(n, vector<int>());
int a, b;
for (int i = 0; i < e; i++)
{
cin >> a >> b;
func(a, b);
}
for (int i = 0; i < n; i++)
{
if (!v[i])
bfs(i);
}
return 0;
}