Pagini recente » Cod sursa (job #482631) | Cod sursa (job #530470) | Cod sursa (job #2102862) | Cod sursa (job #2294157) | Cod sursa (job #2857805)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define ll long long
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
int n, m, s, d[100001];
queue < int > q;
vector < int > v[100001];
bool ver[100001];
void bfs (int nc) {
for (int i = 1; i <= n; i++)
d[i] = -1;
d[nc] = 0;
q.push (nc);
ver[nc] = true;
while (not q.empty ()) {
nc = q.front ();
q.pop ();
for (int i = 0; i < v[nc].size (); i++) {
int nv = v[nc][i];
if (not ver[nv]) {
q.push (nv);
d[nv] = d[nc] + 1;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
fin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int x, y;
fin >> x >> y;
v[x].push_back (y);
}
bfs (s);
for (int i = 1; i <= n; i++)
fout << d[i] << ' ';
return 0;
}