Pagini recente » Cod sursa (job #2953589) | Cod sursa (job #2592775) | Cod sursa (job #1130502) | Cod sursa (job #781134) | Cod sursa (job #3192466)
// 24ian_cerc.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
int N, M, S;
vector <int>G[100005];
int viz[100005] = {0}, dist[100005] = {0};
queue<int>Q;
void bfs()
{
Q.push(S);
viz[S] = 1;
while (!Q.empty())
{
int nodCurent = Q.front(); Q.pop();
for (auto i : G[nodCurent])
{
if (viz[i] == 0)
{
viz[i] = 1;
Q.push(i);
dist[i] = dist[nodCurent] + 1;///costul unei muchii este 1
}
}
}
}
int main()
{
cin >> N >> M >> S;
for (int i = 0; i < M; i++)
{
int x, y;
cin >> x >> y;
G[x].push_back(y);
}
bfs();
for (int i = 1; i <= N; i++)
if (i == S)cout << '0' << " ";
else if (dist[i] == 0)
cout << -1 << " ";
else
cout << dist[i] << " ";
return 0;
}