Pagini recente » Cod sursa (job #621735) | Cod sursa (job #2228523) | Cod sursa (job #433553) | Cod sursa (job #2290910) | Cod sursa (job #2559491)
#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;
while(coada.size()){
int vf = coada.front();
coada.pop();
for(auto x: mat[vf]){
if(dist[x] > dist[vf] + 1){
dist[x] = dist[vf] + 1;
coada.push(x);
}
}
}
}
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;
}