Pagini recente » Cod sursa (job #2413426) | Cod sursa (job #1993345) | Cod sursa (job #218847) | Cod sursa (job #2071187) | Cod sursa (job #1109640)
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream in( "bfs.in" );
ofstream out( "bfs.out" );
const int nmax = 100000;
int v[nmax+1];
int n, m, a, b, s;
vector <int> g[nmax+1];
queue <int> q;
void bfs( int s ) {
q.push( s );
while ( !q.empty() ) {
int w = q.front();
q.pop();
for( int i = 0; i < (int)g[w].size(); ++ i ) {
int x = g[w][i];
if ( v[ x ] == 0 && x != s ) {
v[ x ] = v[ w ] + 1;
q.push( x );
}
}
}
}
int main()
{
int player_unu=0;
in>>n>>m>>s;
for( int i = 0; i < m; ++ i ) {
in>>a>>b;
g[a].push_back( b );
}
bfs( s );
for( int i = 1; i <= n; ++ i ) {
if ( v[i] == 0 && i != s )
out<<"-1 ";
else
out<<v[i]<<' ';
}
out<<'\n';
return player_unu;
}