Pagini recente » Cod sursa (job #2542630) | Cod sursa (job #2364750) | Cod sursa (job #2624034) | Cod sursa (job #2748174) | Cod sursa (job #2974299)
#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];
bool viz[NMAX];
vector<int> g[NMAX];
queue<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;
ans[s] = 0;
qe.push(s);
while (!qe.empty()) {
int x = qe.front();
qe.pop();
viz[x] = true;
for (auto &it : g[x]) {
if (!viz[it]) {
qe.push(it);
ans[it] = ans[x] + 1;
}
}
}
}
int main() {
read();
bfs();
for (int i = 1; i <= n; i++)
fout << ans[i] << ' ';
return 0;
}