Pagini recente » Cod sursa (job #2507119) | Cod sursa (job #1550381) | Cod sursa (job #1176911) | Cod sursa (job #2647065) | Cod sursa (job #2910778)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin( "bfs.in" );
ofstream fout( "bfs.out" );
const int MAX_N = 1e5;
vector <int> g[MAX_N + 1];
int dist[MAX_N + 1];
queue <int> q;
int main() {
int n, m, i, s;
fin >> n >> m >> s;
for ( i = 0; i < m; i++ ) {
int a, b;
fin >> a >> b;
g[a].push_back(b);
}
q.push (s);
dist[s] = 1;
while ( q.size () > 0 ){
int x = q.front();
q.pop ();
for ( i = 0; i < g[x].size (); i++ ){
if ( dist[g[x][i]] == 0 ){
dist[g[x][i]] = dist[x] + 1;
q.push ( g[x][i] );
}
}
}
for ( i = 1; i <= n; i++ )
fout << dist[i] - 1 << ' ';
return 0;
}