Pagini recente » Profil Tudor_Fernea | Atasamentele paginii Poze preONI 2007 - pregatiri | Diferente pentru utilizator/robybrasov intre reviziile 4 si 3 | Monitorul de evaluare | Cod sursa (job #2858049)
#include <fstream>
#include <vector>
#define NMAX 100005
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
int n, m, s, x, y;
int dist[NMAX];
vector <int> vecini[NMAX];
bool viz[NMAX];
void bfs(int i)
{
viz[i] = 1;
for (int j = 0; j < vecini[i].size(); ++j)
{
if (viz[vecini[i][j]] == 0 || dist[i] + 1 < dist[vecini[i][j]])
{
dist[vecini[i][j]] = dist[i] + 1;
bfs(vecini[i][j]);
}
}
}
int main()
{
int i;
in >> n >> m >> s;
for (i = 1; i <= m; ++i)
{
in >> x >> y;
vecini[x].push_back(y);
}
bfs(s);
for (i = 1; i <= n; ++i)
{
if (viz[i] == 0)
out << -1 << " ";
else
out << dist[i] << " ";
}
return 0;
}