Pagini recente » Cod sursa (job #1686289) | Cod sursa (job #2863082) | Cod sursa (job #2513342) | Cod sursa (job #829234) | Cod sursa (job #1921423)
#include <fstream>
#include <vector>
#include <queue>
#define maxn 100002
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector <int> g[maxn];
int viz[maxn], dist[maxn], n, m, s;
void read()
{
int x, y;
fin >> n >> m >> s;
for (int i = 0; i < m; i++) {
fin >> x >> y;
g[x].push_back(y);
}
}
void write()
{
for (int i = 1; i <= n; i++)
if (dist[i] > 0 || i == s)
fout << dist[i] << ' ';
else
fout << "-1" << ' ';
}
void solve()
{
int aux;
queue <int> q;
vector <int>::iterator it;
viz[s] = 1;
q.push(s);
while (!q.empty()) {
aux = q.front();
q.pop();
for (it = g[aux].begin(); it != g[aux].end(); it++)
if (viz[*it] == 0) {
viz[*it] = 1;
dist[*it] = dist[aux] + 1;
q.push(*it);
}
}
}
int main()
{
read();
solve();
write();
return 0;
}