Pagini recente » Cod sursa (job #1413930) | Cod sursa (job #2969682) | Cod sursa (job #1996154) | Cod sursa (job #721251) | Cod sursa (job #1862476)
#include <stdio.h>
#include <vector>
#include <deque>
#define pb push_back
#define NMAX 100001
using namespace std;
FILE *f = freopen("bfs.in", "r", stdin);
FILE *g = freopen("bfs.out", "w", stdout);
vector <int> G[NMAX];
deque <int> q;
int cost[NMAX];
int n, m, s;
void bfs(int start) {
for(int i = 0; i<NMAX; i++)
cost[i] = -1;
q.pb(start);
cost[start] = 0;
while(!q.empty()) {
int nod = q.front();
q.pop_front();
for(int i = 0; i<G[nod].size(); i++)
if(cost[G[nod][i]] == -1) {
q.pb(G[nod][i]);
cost[G[nod][i]] = cost[nod] + 1;
}
}
}
void read() {
scanf("%d%d%d", &n, &m, &s);
for(int i = 1; i<=m; i++) {
int x, y;
scanf("%d%d", &x, &y);
G[x].pb(y);
}
}
int main() {
read();
bfs(s);
for(int i = 1; i<=n; i++)
printf("%d ", cost[i]);
return 0;
}