Pagini recente » Rating infoarena | Cod sursa (job #3294935) | Cod sursa (job #3032627) | Cod sursa (job #2927365) | Cod sursa (job #3291402)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
int n, m, x, y, S, f[100005], d[100005];
queue<int> bfs_queue;
vector<int> L[100005];
void bfs (int nod) {
bfs_queue.push(nod);
f[nod] = 1;
d[nod] = 0;
while (!bfs_queue.empty()){
int last = bfs_queue.front();
bfs_queue.pop();
for (int i=0; i<L[last].size(); i++){
int neighbour = L[last][i];
if (f[neighbour] == 0){
f[neighbour] = 1;
d[neighbour] = d[last] + 1;
bfs_queue.push(neighbour);
}
}
}
}
int main(){
fin >> n >> m >> S;
for (int i=0; i<m; i++){
fin >> x >> y;
L[x].push_back(y);
}
for (int i=1; i<=n; i++){
d[i] = -1;
}
bfs(S);
for (int i=1; i<=n; i++){
fout << d[i] << ' ';
}
return 0;
}