Pagini recente » Cod sursa (job #2452754) | Cod sursa (job #963289) | Cod sursa (job #2807513) | Cod sursa (job #1796954) | Cod sursa (job #2924817)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
fstream fin("bfs.in");
fstream fout("bfs.out");
int main(){
int n, m, s, x, y;
fin >> n >> m >> s;
vector<vector<int>> G(n + 1);
vector<int> d(n + 1, -1);
for(int i = 0; i < m; i ++){
fin >> x >> y;
G[x].push_back(y);
}
queue<int> q;
q.push(s);
d[s] = 0;
while(!q.empty()){
x = q.front();
q.pop();
for(const auto& y: G[x])
if(d[y] == -1){
d[y] = d[x] + 1;
q.push(y);
}
}
for(int i = 1; i <= n; i ++)
fout << d[i] << " ";
return 0;
}