Pagini recente » Cod sursa (job #1792054) | Rating Barbulescu (adi_barbulescu99) | Cod sursa (job #1489335) | Cod sursa (job #1249839) | Cod sursa (job #1899528)
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int n, m, s, q[1000005], t[1000005], nod[1000005];
vector <int> g[100005];
inline void BFS(int nodi){
int st = 1, dr = 1, timp = 0;
while(st <= dr){
if(q[st] == nodi) {printf("%d ", t[st]); return;}
for(int i = 0; i < g[q[st]].size() ; ++i){
if(nod[g[q[st]][i]] == 0){
q[++dr] = g[q[st]][i]; t[dr] = t[st] + 1;
nod[g[q[st]][i]] = 1;
continue ;
}
continue ;
}
++st;
}
printf("-1 ");
}
int main()
{
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
scanf("%d%d%d", &n, &m, &s);
int x, y;
for(int i = 1; i <= m ; ++i){
scanf("%d%d", &x, &y);
g[x].push_back(y);
}
for(int i = 1; i <= n ; ++i){
if(i == s) printf("0 ");
else{
q[1] = s;
memset(nod, 0, sizeof(nod));
nod[s] = 1;
BFS(i);
}
}
return 0;
}