Pagini recente » Cod sursa (job #252105) | Cod sursa (job #2617555) | Cod sursa (job #1264313) | Cod sursa (job #1920461) | Cod sursa (job #1775123)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "fstream"
#include "vector"
#include "queue"
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int inf = 1000000005;
const int nmax = 100005;
vector<int> graph[nmax];
vector <int>::iterator ii;
queue<int> q;
int dist[nmax];
int main() {
int n, m, s;
fin >> n >> m >> s;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
for (int i = 1; i <= n; i++)
{
if (i != s) {
dist[i] = inf;
}
}
q.push(s);
while (!q.empty()) {
int node = q.front();
q.pop();
for (int ii = 0; ii != graph[node].size(); ++ii)
{
if (dist[node] + 1 < dist[graph[node][ii]])
{
dist[graph[node][ii]] = dist[node] + 1;
q.push(graph[node][ii]);
}
}
}
for (int i = 1; i <= n; i++)
if (dist[i] == inf)
fout << "-1 ";
else
fout << dist[i] << " ";
fout << "\n";
fin.close();
fout.close();
return 0;
}