Pagini recente » Cod sursa (job #1371073) | Profil 05_Yohn | Profil 05_Yohn | Borderou de evaluare (job #1181953) | Cod sursa (job #3306744)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> a[100005];
int ans[100005];
void bfs(int k) {
queue<int> q;
q.push(k);
while(q.size()) {
for(auto i : a[q.front()]) {
if(ans[i] == -1) {
ans[i] = ans[q.front()] + 1;
q.push(i);
}
}
q.pop();
}
}
int main() {
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k; cin >> n >> m >> k;
for(int i = 1; i <= m; i ++) {
int x, y; cin >> x >> y;
a[x].push_back(y);
}
for(int i = 1; i <= n; i ++) {
ans[i] = -1;
}
ans[k] = 0;
bfs(k);
for(int i = 1; i <= n; i ++) {
cout << ans[i] << ' ';
}
}