-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultibfs.cpp
More file actions
58 lines (47 loc) · 852 Bytes
/
multibfs.cpp
File metadata and controls
58 lines (47 loc) · 852 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
#include <iostream>
#include <iterator>
#include <queue>
#include <vector>
using namespace std;
int n;
vector<vector<int>> g;
vector<int> d;
enum { UNVISITED = -1 };
void bfs(const vector<int> &start) {
queue<int> q;
for (auto e : start) {
q.push(e);
d[e] = 0;
}
while (!q.empty()) {
int t = q.front();
q.pop();
for (auto e : g[t]) {
if (d[e] == UNVISITED) {
d[e] = d[t] + 1;
q.push(e);
}
}
}
}
int main() {
int m;
cin >> n >> m;
g.assign(n, vector<int>());
d.assign(n, UNVISITED);
while (m--) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int s;
cin >> s;
vector<int> start(s, 0);
for (auto &e : start)
cin >> e;
bfs(start);
copy(begin(d), end(d), ostream_iterator<int>(cout, " "));
cout << '\n';
return 0;
}