Pagini recente » Cod sursa (job #2570411) | autumn-warmup-2007/clasament/runda-1 | Cod sursa (job #2966753) | Cod sursa (job #393002) | Cod sursa (job #3210610)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
const int NMAX = 1e5+1;
int costuri[NMAX];
queue<int> coada;
int n, m,t;
vector<int> graf[NMAX];
void citire() {
f >> n >> m >> t;
for (int i = 0; i < m; i++) {
int x, y;
f >> x >> y;
graf[x].push_back(y);
}
}
void afisare() {
for (int i = 1; i <= n; i++) {
g << costuri[i] << " ";
}
}
void bfs(int start) {
coada.push(start);
for (int i = 1; i <=n+1; i++)costuri[i] = -1;
costuri[start] = 0;
while (!coada.empty()) {
int cur = coada.front();
coada.pop();
for (auto e : graf[cur]) {
if (costuri[e] == -1)
{
coada.push(e);
costuri[e] = costuri[cur]+1;
}
}
}
}
int main()
{
citire();
bfs(t);
afisare();
}