Pagini recente » Cod sursa (job #1838956) | Cod sursa (job #2546041) | Cod sursa (job #206036) | Cod sursa (job #2686542) | Cod sursa (job #2839742)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
const int NMAX = 100001;
int n, m, s;
vector < int > ad[NMAX];
queue < int > Q;
int vis[NMAX];
void read()
{
fin >> n >> m >> s;
for (int i = 1; i <= m; ++i)
{
int x, y;
fin >> x >> y;
ad[x].push_back(y);
}
}
void bfs(int nod)
{
Q.push(nod);
vis[nod] = 1;
while (!Q.empty())
{
int v = Q.front();
Q.pop();
for (int i = 0; i < ad[v].size(); ++i)
{
int w = ad[v][i];
if (vis[w] == 0)
{
Q.push(w);
vis[w] = vis[v] + 1;
}
}
}
}
int main()
{
read();
bfs(s);
for (int i = 1; i <= n; ++i)
{
if (vis[i] == 0) fout << -1 << ' ';
else fout << vis [i] - 1 << ' ';
}
return 0;
}