Pagini recente » Cod sursa (job #2824428) | Cod sursa (job #7145) | Cod sursa (job #3122457) | Infoarena Monthly 2014 - Runda 11 | Cod sursa (job #2964628)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
vector <int> d(100001, -1), a[100001];
void bfs(int s){
queue <int> q;
d[s] = 0;
q.push(s);
while(!q.empty()){
int x = q.front();
q.pop();
for(auto y:a[x]){
if(d[y] == -1){
d[y] = d[x] + 1;
q.push(y);
}
}
}
}
int main()
{
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s;
fin >> n >> m >> s;
for(int i = 0; i < m; i++){
int x, y;
fin >> x >> y;
a[x].push_back(y);
}
bfs(s);
for(int i = 1; i <= n; i++)
fout << d[i] << " ";
return 0;
}