Pagini recente » Cod sursa (job #1349079) | Cod sursa (job #1075928) | Cod sursa (job #831083) | Cod sursa (job #1462394) | Cod sursa (job #1897978)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int main()
{
int n, m, s;
fin>> n >> m >> s;
vector<int> p[n + 1];
vector<int> res(n + 1, -1);
for(int i = 0 ; i < m; i++) {
int x, y;
fin >> x >> y;
p[x].push_back(y);
}
res[s] = 0;
queue<int> q;
q.push(s);
while(!q.empty()) {
int x = q.front();
q.pop();
for(int aux:p[x]) {
res[aux] = res[x] + 1;
q.push(aux);
}
}
for(int i = 1; i < n+1; i++) {
fout << res[i] << " ";
}
return 0;
}