Pagini recente » Autentificare | Istoria paginii runda/simulare_de_oni_5/clasament | Istoria paginii runda/simulare-oji-xutzu/clasament | Cod sursa (job #1002076) | Cod sursa (job #1034110)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
const int MAXN = 100010;
vector <int> G[MAXN];
queue <int> q;
int N, M, S, dist[MAXN];
void citire ()
{
int x, y;
fin >> N >> M >> S;
for (int i = 0; i < M; ++i)
{
fin >> x >> y;
G[x].push_back(y);
}
dist[S] = 1;
q.push(S);
}
void bfs ()
{
while ( !q.empty() )
{
int x = q.front();
int L = G[x].size();
q.pop();
for (int i = 0; i < L; ++i)
{
int y = G[x][i];
if ( dist[y] == 0)
{
dist[y] = dist[x] + 1;
q.push(y);
}
}
}
}
void afisare ()
{
for (int i = 1; i <= N; ++i)
fout << dist[i] - 1 << " ";
}
int main ()
{
citire();
bfs();
afisare();
fin.close ();
fout.close ();
return 0;
}