Pagini recente » Cod sursa (job #1697711) | Cod sursa (job #570758) | Cod sursa (job #2151611) | Cod sursa (job #2984135) | Cod sursa (job #2817251)
#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 (int i = 0; i < gr[front].size(); i++)
if (!ans[gr[front][i]]) {
ans[gr[front][i]] = ans[front] + 1;
q.push(gr[front][i]);
}
q.pop();
}
for (int i = 1; i <= n; i++) out << ans[i] - 1 << " ";
out << '\n';
return 0;
}