Pagini recente » Cod sursa (job #1820809) | Cod sursa (job #2672186) | Cod sursa (job #3002069) | Cod sursa (job #619456) | Cod sursa (job #2669551)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
const int NMAX = 100000;
const int INF = 10 * NMAX;
int N, M, S;
vector <int> g[NMAX + 2];
int dist[NMAX + 2];
int main()
{
cin >> N >> M >> S;
for(int i = 1; i <= M; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
}
for(int i = 1; i <= N; i++)
dist[i] = INF;
queue <int> q;
q.push(S);
dist[S] = 0;
while(!q.empty()) {
int node = q.front();
q.pop();
for(auto it : g[node])
if(dist[it] == INF) {
dist[it] = dist[node] + 1;
q.push(it);
}
}
for(int i = 1; i <= N; i++)
if(dist[i] == INF) cout << -1 << ' ';
else cout << dist[i] << ' ';
return 0;
}