Pagini recente » Cod sursa (job #2230956) | Cod sursa (job #146677) | Cod sursa (job #1089140) | Cod sursa (job #852352) | Cod sursa (job #2792835)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin ( "bfs.in" );
ofstream cout ( "bfs.out" );
#define NMAX 100000
#define MMAX 1000000
vector<int> v[NMAX];
queue<int> BFS;
int vizitat[MMAX], query;
int ans[NMAX], dist;
void bfs() {
int elem;
while ( !BFS.empty() ) {
elem = BFS.front();
for ( auto copil: v[elem] ) {
if ( vizitat[copil] == 0 ) {
BFS.push(copil);
ans[copil] = dist + 1;
vizitat[copil] = 1;
}
}
dist++;
BFS.pop();
}
}
int main() {
int n, m, s, i, x, y;
cin >> n >> m >> s;
for ( i = 0; i < m; i++ ) {
cin >> x >> y;
v[x].push_back(y);
}
ans[s] = 1;
vizitat[s] = 1;
dist = 1;
BFS.push(s);
bfs();
for ( i = 1; i <= n; i++ ) {
if ( ans[i] == 0 ) {
cout << "-1 ";
}
else {
cout << ans[i] - 1 << " ";
}
}
return 0;
}