Pagini recente » Cod sursa (job #3367098) | Cod sursa (job #3367058) | Cod sursa (job #3367180) | Cod sursa (job #3367117) | Cod sursa (job #3367059)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfs.out");
int n, m, s;
vector<int> G[100001];
int d[100001];
void bfs() {
queue<int> q;
q.push(s);
for(int i = 1; i <= n; ++i) {
d[i] = 2e9;
}
d[s] = 0;
while(!q.empty()) {
int nod = q.front();
q.pop();
for(auto nodnou : G[nod]) {
if(d[nod] + 1 < d[nodnou]) {
d[nodnou] = d[nod] + 1;
q.push(nodnou);
}
}
}
}
int main() {
cin >> n >> m >> s;
for(int i = 1; i <= m; ++i) {
int x, y; cin >> x >> y;
G[x].push_back(y);
}
bfs();
for(int i = 1; i <= n; ++i) {
if(d[i] == (int) 2e9) cout << "-1 ";
else cout << d[i] << ' ';
}
return 0;
}