Pagini recente » Cod sursa (job #1459682) | Cod sursa (job #2892583) | Cod sursa (job #800949) | Cod sursa (job #1132724) | Cod sursa (job #2722034)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int NMax = 1e5, oo = 0x3f3f3f3f;
vector <int> g[NMax + 5];
queue <int> q;
int n, m, source;
int dist[NMax + 5];
void Read(){
fin >> n >> m >> source;
while (m--){
int x, y;
fin >> x >> y;
g[x].push_back(y);
}
}
void Solve(){
memset(dist, oo, sizeof dist);
dist[source] = 0;
q.push(source);
while (!q.empty()){
int node = q.front();
q.pop();
for (auto ngh : g[node])
if (dist[ngh] == oo){
dist[ngh] = dist[node] + 1;
q.push(ngh);
}
}
}
void Print(){
for (int i = 1; i <= n; i++){
if (dist[i] == oo)
fout << -1 << ' ';
else
fout << dist[i] << ' ';
}
fout << '\n';
}
int main(){
Read();
Solve();
Print();
return 0;
}