Pagini recente » Cod sursa (job #477818) | Cod sursa (job #2463970) | Cod sursa (job #90285) | Cod sursa (job #2032578) | Cod sursa (job #3134088)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 100010
ifstream f("bfs.in");
ofstream g("bfs.out");
vector<int> G[MAXN];
int dist[MAXN], n, m, nr, x, y, start;
queue<int> Q;
void bfs(int nod) {
for (int i = 1; i <= n; ++i) {
dist[i] = -1;
}
dist[nod] = 0;
Q.push(nod);
while (Q.size()) {
int aux = Q.front();
Q.pop();
for (auto it:G[aux]) {
if (dist[it] == -1) {
Q.push(it);
dist[it] = dist[aux] + 1;
}
}
}
}
int main()
{
f >> n >> m >> start;
while (m--) {
f >> x >> y;
G[x].push_back(y);
}
bfs(start);
for (int i = 1; i <= n; ++i) {
g << dist[i] << ' ';
}
return 0;
}