Pagini recente » Cod sursa (job #1931269) | Cod sursa (job #837552) | Cod sursa (job #2347019) | Cod sursa (job #3138752) | Cod sursa (job #2974300)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#ifdef LOCAL
ifstream fin("input.txt");
#define fout cout
#else
#include <bits/stdc++.h>
ifstream fin("bfs.in");
ofstream fout("bfs.out");
#endif
const int NMAX = 1e5+5;
int n, m, s;
int ans[NMAX];
vector<int> g[NMAX];
queue<pair<int, int>> qe;
void read() {
fin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int x, y;
fin >> x >> y;
g[x].push_back(y);
}
}
void bfs() {
for (int i = 1; i <= n; i++)
ans[i] = -1;
qe.push({s, 0});
while (!qe.empty()) {
int x = qe.front().first;
int c = qe.front().second;
qe.pop();
ans[x] = c;
for (auto &it : g[x])
if (ans[it] == -1)
qe.push({it, c + 1});
}
}
int main() {
read();
bfs();
for (int i = 1; i <= n; i++)
fout << ans[i] << ' ';
return 0;
}