-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
48 lines (41 loc) · 1.3 KB
/
Copy pathbfs.cpp
File metadata and controls
48 lines (41 loc) · 1.3 KB
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
#include <bits/stdc++.h>
using namespace std;
// Modern, compact BFS suitable for competitive programming (C++23)
// Reads a directed graph (n, m) followed by m edges (0-based by default),
// runs BFS from source s (default 0) and prints distances (-1 = unreachable).
pair<vector<int>, vector<int>> bfs(const vector<vector<int>>& g, int s) {
int n = (int)g.size();
vector<int> dist(n, -1), pred(n, -1);
queue<int> q;
dist[s] = 0;
q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v : g[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
pred[v] = u;
q.push(v);
}
}
}
return {dist, pred};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
if (!(cin >> n >> m)) return 0;
vector<vector<int>> g(n);
for (int i = 0; i < m; ++i) {
int u, v; cin >> u >> v;
// If input is 1-based, uncomment: // --u; --v;
g[u].push_back(v);
// For undirected graphs also add: // g[v].push_back(u);
}
int s = 0; // change source if needed or read from input
auto [dist, pred] = bfs(g, s);
// Output distances: index: distance
for (int i = 0; i < n; ++i) cout << i << ": " << dist[i] << '\n';
return 0;
}