1vector<vector<int>> adj; // adjacency list representation
2int n; // number of nodes
3int s; // source vertex
4
5queue<int> q;
6vector<bool> used(n);
7vector<int> d(n), p(n);
8
9q.push(s);
10used[s] = true;
11p[s] = -1;
12while (!q.empty()) {
13 int v = q.front();
14 q.pop();
15 for (int u : adj[v]) {
16 if (!used[u]) {
17 used[u] = true;
18 q.push(u);
19 d[u] = d[v] + 1;
20 p[u] = v;
21 }
22 }
23}
24