Pagini recente » Cod sursa (job #3367180) | Cod sursa (job #3367117) | Cod sursa (job #3362553) | Cod sursa (job #3367059) | Cod sursa (job #3367060)
#include <bits/stdc++.h>
#define MAXN 100001
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int main() {
int n, m, s;
int result[MAXN];
fin >> n >> m >> s;
vector<vector<int>> graph(n + 1);
for (int i = 0; i < m; i++) {
int from, to;
fin >> from >> to;
graph[from].push_back(to);
}
for (int i = 1; i <= n; i++) {
result[i] = INT_MAX;
}
queue<int> q;
result[s] = 0;
q.push(s);
while (!q.empty()) {
int current = q.front();
q.pop();
for (int i = 0; i < graph[current].size(); i++) {
if (result[graph[current][i]] == INT_MAX) {
result[graph[current][i]] = result[current] + 1;
q.push(graph[current][i]);
}
}
}
for (int i = 1; i <= n; i++) {
fout << (result[i] == INT_MAX ? -1 : result[i]) << ' ';
}
fout << '\n';
return 0;
}