Pagini recente » Cod sursa (job #671959) | Cod sursa (job #1095215) | Cod sursa (job #645269) | Cod sursa (job #2909820) | Cod sursa (job #2817273)
#include <bits/stdc++.h>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int N_MAX = 1e5;
int n, m, s;
vector<int> gr[N_MAX + 5];
int ans[N_MAX + 5];
int main() {
in >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int x, y;
in >> x >> y;
gr[x].push_back(y);
}
queue<int> q;
q.push(s);
ans[s] = 1;
while (!q.empty()) {
int front = q.front();
for (auto adj : gr[front])
if (!ans[adj]) {
ans[adj] = ans[front] + 1;
q.push(adj);
}
q.pop();
}
for (int i = 1; i <= n; i++) out << ans[i] - 1 << " ";
out << '\n';
return 0;
}