Pagini recente » Cod sursa (job #917308) | Cod sursa (job #238023) | Cod sursa (job #2989475) | Cod sursa (job #301278) | Cod sursa (job #2566842)
#include <bits/stdc++.h>
using namespace std;
const int len = 100005;
//const int inf = 1 << 30;
int m, n, start, x, y, dist[len];
vector<int> g[len];
//vector<int> sol;
queue<int> q;
void bfs(int start) {
for (int i = 1; i <= n; i++)
dist[i] = -1;
dist[start] = 0;
q.push(start);
while (!q.empty()) {
int curr = q.front();
q.pop();
for (auto next : g[curr])
if (dist[next] == -1) {
dist[next] = dist[curr] + 1;
q.push(next);
//sol.push_back(next);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
cin >> n >> m >> start;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
g[x].push_back(y);
}
bfs(start);
for (int i = 1; i <= n; i++)
cout << dist[i] << " ";
cout << "\n";
//for (int it : sol)
// cout << it << " ";
}