Pagini recente » Cod sursa (job #792446) | Cod sursa (job #2453469) | Cod sursa (job #1680961) | Cod sursa (job #2451280) | Cod sursa (job #2559518)
#include<bits/stdc++.h>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector<int> mat[100005];
queue<int> coada;
int dist[100005];
void BFS(int nod_plecare){
coada.push(nod_plecare);
dist[nod_plecare] = 0;
int nodcurent, vecin;
while(!coada.empty()){
nodcurent= coada.front();
coada.pop();
for(unsigned int i = 0; i < mat[nodcurent].size(); i++){
vecin = mat[nodcurent][i];
if(dist[vecin] == -1){
coada.push(vecin);
dist[vecin] = dist[nodcurent] + 1;
}
}
}
}
int main()
{
int n, m, s, x, y;
in >> n >> m >> s;
for(int i = 1; i <= n; i++){
dist[i] = -1;
}
for(int i = 1; i <= m; i++){
in >> x >> y;
mat[x].push_back(y);
}
BFS(s);
for(int i = 1; i <= n; i++){
out << dist[i] <<' ';
}
return 0;
}