Pagini recente » Cod sursa (job #652074) | Cod sursa (job #2854377) | Cod sursa (job #1047544) | Cod sursa (job #2816508) | Cod sursa (job #2597060)
#include <bits/stdc++.h>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
int n, m, s, ans[100005], x, y;
vector <int> graph[100005];
void bfs() {
queue <int> depthSearch;
depthSearch.push(s);
memset(ans, -1, sizeof(ans));
ans[s] = 0;
while(depthSearch.size()) {
int adj = depthSearch.front();
depthSearch.pop();
for(int a: graph[adj]) {
if(ans[a] == -1) {
ans[a] = ans[adj] + 1;
depthSearch.push(a);
}
}
}
}
int main()
{
in >> n >> m >> s;
for(int i = 1; i <= m; i++) {
in >> x >> y;
graph[x].push_back(y);
}
bfs();
for(int i = 1; i <= n; i++) {
out << ans[i] << ' ';
}
return 0;
}