Pagini recente » Cod sursa (job #689126) | Cod sursa (job #661711) | Cod sursa (job #2755474) | Cod sursa (job #2623572) | Cod sursa (job #1705399)
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int ap[100010];
queue <int> q;
vector <int> v[100010];
inline void bfs (int nod)
{
q.push (nod);
ap[nod] = 1;
while (!q.empty ())
{
int x = q.front ();
q.pop ();
for (auto &it : v[x])
if (!ap[it])
{
q.push (it);
ap[it] = ap[x] + 1;
}
}
}
int main ()
{
freopen ("bfs.in", "r", stdin);
freopen ("bfs.out", "w", stdout);
int n, m, S;
scanf ("%d %d %d", &n, &m, &S);
for (int i = 1; i <= m; ++i)
{
int x, y;
scanf ("%d %d", &x, &y);
v[x].push_back (y);
}
bfs (S);
for (int i = 1; i <= n; ++i)
printf ("%d ", ap[i] - 1);
printf ("\n");
return 0;
}