Pagini recente » Cod sursa (job #2763761) | Cod sursa (job #2499902) | Cod sursa (job #3167210) | Cod sursa (job #1670493) | Cod sursa (job #2527905)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
#define MAXN 100005
queue <int> q;
vector <int> edge[MAXN];
int m, n, x, y, current, start;
int dist[MAXN];
void bfs() {
while(!q.empty()) {
current = q.front();
q.pop();
for (size_t i = 0; i < edge[current].size(); i++) {
int next = edge[current][i];
if (dist[next] == -1) {
dist[next] = dist[current] + 1;
q.push(next);
}
}
}
}
int main() {
fin >> n >> m >> start;
for (int i = 1; i <= m; i++) {
fin >> x >> y;
edge[x].push_back(y);
}
for (int i = 1; i <= n; i++)
dist[i] = -1;
dist[start] = 0;
q.push(start);
bfs();
for (int i = 1; i <= n; i++)
fout << dist[i] << " ";
}