Pagini recente » Cod sursa (job #797916) | Cod sursa (job #3256126) | Cod sursa (job #3203413) | Cod sursa (job #2566406) | Cod sursa (job #2532677)
#include <bits/stdc++.h>
using namespace std;
const int len = 100005;
int n, m, start, x, y, curr, dist[len];
queue<int> q;
vector<int> g[len];
void bfs() {
while (!q.empty()) {
curr = q.front();
q.pop();
for (auto it : g[curr])
if (dist[it] == -1) {
dist[it] = dist[curr] + 1;
q.push(it);
}
}
}
void write() {
memset(dist, -1, sizeof(dist));
dist[start] = 0;
q.push(start);
bfs();
for (int i = 1; i <= n; i++)
cout << dist[i] << " ";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
cin >> n >> m >> start;
for (int i = 0; i < m; i++) {
cin >> x >> y;
g[x].push_back(y);
}
write();
}