Pagini recente » Cod sursa (job #1784811) | Cod sursa (job #2326001) | Cod sursa (job #799433) | Cod sursa (job #1854812) | Cod sursa (job #3156438)
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
const int NMAX = 100000;
vector<int> G[NMAX + 1];
int d[NMAX + 1];
int vis[NMAX + 1];
ifstream fin("bfs.in");
ofstream fout("bfs.out");
void BFS(int x) {
queue<int> q;
q.push(x);
d[x] = 0;
vis[x] = 1;
while (!q.empty()) {
x = q.front();
q.pop();
for (auto next : G[x])
{
if (!vis[next])
{
q.push(next);
vis[next] = 1;
d[next] = d[x] + 1;
}
}
}
}
int main()
{
int n, m, S;
fin >> n >> m >> S;
int x, y;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
G[x].push_back(y);
}
BFS(S);
for (int i = 1; i <= n; i++) {
if (i != S && d[i] == 0)
fout << "-1 ";
else
fout << d[i] << " ";
}
return 0;
}