Pagini recente » Cod sursa (job #2393561) | Cod sursa (job #2852126) | Cod sursa (job #2606569) | Cod sursa (job #1167277) | Cod sursa (job #1194359)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
#define maxN 100005
#define inf (1 << 30)
#define PB push_back
queue <int> Q;
vector <int> list[maxN];
int cost[maxN];
int N, M, S;
void bfs() {
for(int i = 1; i <= N; ++ i)
cost[i] = inf;
Q.push(S);
cost[S] = 0;
while(! Q.empty()) {
int nod = Q.front();
Q.pop();
for(int i = 0; i < list[nod].size(); ++ i) {
int nod2 = list[nod][i];
if(cost[nod2] != inf) continue;
cost[nod2] = cost[nod] + 1;
Q.push(nod2);
}
}
}
int main() {
ifstream f("bfs.in");
ofstream g("bfs.out");
f >> N >> M >> S;
while(M --) {
int x, y;
f >> x >> y;
list[x].PB(y);
}
bfs();
for(int i = 1; i <= N; ++ i)
if(cost[i] == inf) g << "-1 ";
else g << cost[i] << " ";
return 0;
}