Pagini recente » Cod sursa (job #1475068) | Cod sursa (job #1180795) | Cod sursa (job #1880858) | Cod sursa (job #1666841) | Cod sursa (job #3121326)
#include <bits/stdc++.h>
using namespace std;
ifstream in ("BFS.in");
ofstream out ("BFS.out");
const int NMAX = 1e5;
int d[NMAX + 3];
vector<int>v[NMAX + 3];
void bfs (int x)
{
queue<int>q;
q.push(x);
d[x] = 0;
while (!q.empty())
{
int pos = q.front();
q.pop();
cout << pos << ' ';
for (auto it : v[pos])
{
if (d[it] == -1)
{
q.push(it);
d[it] = d[pos] + 1;
}
}
}
}
int main()
{
int n, m, nod;
int x, y;
in >> n >> m >> nod;
while (m--)
{
int x, y;
in >> x >> y;
v[x].push_back(y);
}
memset(d, -1, sizeof(d));
bfs(nod);
for (int i=1; i<=n; i++)
out << d[i] << ' ';
return 0;
}