Pagini recente » Cod sursa (job #2276272) | Cod sursa (job #3255715) | Cod sursa (job #3192175) | Cod sursa (job #143306) | Cod sursa (job #1985548)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define pb push_back
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int NLIM = 1e5 + 10;
int N, M;
int ST;
vector< int > graph[NLIM];
int bfs[NLIM];
int main()
{
fin >> N >> M >> ST;
for( int i = 0; i < M; ++i )
{
int x, y;
fin >> x >> y;
graph[x].pb( y );
//graph[y].pb( x );
}
queue< int > qu;
bfs[ST] = 1;
qu.push( ST );
while( qu.size() )
{
int nod = qu.front();
for( int j = 0; j < graph[nod].size(); ++j )
{
int nnod = graph[nod][j];
if( !bfs[nnod] )
{
bfs[nnod] = bfs[nod] + 1;
qu.push( nnod );
}
}
qu.pop();
}
for( int i = 1; i <= N; ++i )
{
fout << bfs[i] - 1 << " ";
}
return 0;
}