Pagini recente » Cod sursa (job #3222205) | Cod sursa (job #1499306) | Cod sursa (job #71582) | Cod sursa (job #857663) | Cod sursa (job #1484190)
#include <queue>
#include <fstream>
#include <vector>
using namespace std;
vector<int>Nout[100010];
int v[100010];
int N, M, S,y,x;
void bfs(){
v[S] = 0;
queue<int>q;
q.push(S);
while (!q.empty()){
x = q.front();
q.pop();
for (auto& i : Nout[x])
if (v[i] == -1){
q.push(i);
v[i] = v[x] + 1;
}
}
}
int main(){
memset(v, -1, sizeof(v));
ifstream f("bfs.in");
ofstream of("bfs.out");
f >> N >> M >> S;
for (int i = 0; i < M; ++i){
f >> x >> y;
Nout[x].push_back(y);
}
bfs();
for (int i = 1; i <= N; ++i){
of << v[i] << " ";
}
return 0;
}