-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBFS.cpp
More file actions
36 lines (32 loc) · 677 Bytes
/
BFS.cpp
File metadata and controls
36 lines (32 loc) · 677 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
vec<int> d(mxN), p(mxN), vis(mxN);
void bfs(int s, int n, vec<vec<int>> &g) {
queue<int> q;
rep(i, 1, n) {
d[i] = p[i] = vis[i] = 0;
}
q.push(s);
vis[s] = 1;
p[s] = -1;
while(sz(q)) {
int v = q.front();
q.pop();
for (int u : g[v]) {
if (!vis[u]) {
vis[u] = 1;
q.push(u);
d[u] = d[v] + 1;
p[u] = v;
}
}
}
}
vec<int> path_retrieve(int u) {
if(!vis[u]) {
return {};
}
vector<int> path;
for(int v = u; v != -1; v = p[v])
path.pb(v);
reverse(all(path));
return path;
}