Pagini recente » Cod sursa (job #2265257) | Cod sursa (job #891709) | Cod sursa (job #3299706) | Cod sursa (job #2838063) | Cod sursa (job #3300682)
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100005, inf = 1e9;
int n, m, s, dist[maxN];
vector <int> G[maxN];
bool viz[maxN];
void read() {
cin >> n >> m >> s;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
}
}
void bfs() {
for (int i = 1; i <= n; i++) {
dist[i] = -1;
}
queue <int> q;
q.push(s);
dist[s] = 0;
viz[s] = 1;
while (!q.empty()) {
int nod = q.front();
q.pop();
for (int vecin : G[nod]) {
if (!viz[vecin]) {
dist[vecin] = dist[nod] + 1;
q.push(vecin);
viz[vecin] = 1;
}
}
}
}
void print() {
for (int i = 1; i <= n; i++) {
cout << dist[i] << ' ';
}
}
int main() {
#ifdef LOCAL
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#else
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
#endif
read();
bfs();
print();
return 0;
}