Pagini recente » Cod sursa (job #2778166) | Cod sursa (job #1655263) | Cod sursa (job #349817) | Cod sursa (job #3144568) | Cod sursa (job #2740649)
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
const int Nmax = 1e5;
int N, M, nodStart, x, y, distante_nod[Nmax + 1];
vector <int> muchii[Nmax + 1];
queue <int> q;
void BFS(){
int nod;
while(!q.empty()){
nod = q.front();
q.pop();
for(int i = 0; i < muchii[nod].size(); ++i){
if(distante_nod[muchii[nod][i]] == -1){
q.push(muchii[nod][i]);
distante_nod[muchii[nod][i]] = distante_nod[nod] + 1;
}
}
}
}
int main(){
cin >> N >> M >> nodStart;
while(M--){
cin >> x >> y;
muchii[x].push_back(y);
}
memset(distante_nod, -1, sizeof(distante_nod));
distante_nod[nodStart] = 0;
q.push(nodStart);
BFS();
for(int i = 1; i <= N; ++i){
cout << distante_nod[i] << " ";
}
return 0;
}