Pagini recente » Cod sursa (job #251906) | Cod sursa (job #1146391) | Cod sursa (job #2740513) | Cod sursa (job #1186602) | Cod sursa (job #1485747)
#include <string.h>
#include <stdio.h>
#include <vector>
#include <deque>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MAX_M = 1e6 + 5;
int N, M, S, G[MAX_N], Cost[MAX_N], A[MAX_N];
vector<int> Graph[MAX_N]; // lista de adiacenta pentru graful G
deque<int> Q;
void BFS(int nod) {
memset(Cost, -1, sizeof(Cost));
Cost[nod] = 0;
Q.push_back(nod);
while(!Q.empty()) {
int nodCurent = Q.front();
Q.pop_front();
for(register int i = 0; i < G[nodCurent]; ++ i)
if(Cost[Graph[nodCurent][i]] == -1) {
Q.push_back(Graph[nodCurent][i]);
Cost[ Graph[nodCurent][i] ] = Cost[nodCurent] + 1;
}
}
}
int main() {
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
scanf("%d %d %d", &N, &M, &S);
int x, y;
for(register int i = 1; i <= M; ++ i) {
scanf("%d %d", &x, &y);
Graph[x].push_back(y);
}
for(register int i = 1; i <= N; ++ i)
G[i] = Graph[i].size();
BFS(S);
for(register int i = 1; i <= N; ++ i)
printf("%d ", Cost[i]);
printf("\n");
return 0;
}