Pagini recente » Cod sursa (job #664512) | Cod sursa (job #2018120) | Cod sursa (job #2374766) | Cod sursa (job #2689918) | Cod sursa (job #976445)
Cod sursa(job #976445)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n, m, s;
vector <int> G[100100];
queue <int> coada;
int dist[100100];
int main()
{
f >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int x, y;
f >> x >> y;
G[x].push_back(y);
//G[y].push_back(x);
}
dist[s] = 1;
coada.push(s);
while (coada.size()) {
for (int i = 0; i < G[coada.front()].size(); i++) {
if (dist[G[coada.front()][i]] == 0) {
dist[G[coada.front()][i]] = dist[coada.front()] + 1;
coada.push(G[coada.front()][i]);
}
}
coada.pop();
}
for (int i = 1; i <= n; i++)
cout << dist[i] - 1 << ' ';
return 0;
}