Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #272883) | Cod sursa (job #929077) | Cod sursa (job #1899530)
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int n, m, s, q[1000005], t[1000005], nod[1000005], d[1000005];
vector <int> g[100005];
inline void BFS(){
int st = 1, dr = 1, timp = 0;
while(st <= dr){
d[q[st]] = t[st];
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;
}
}
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);
}
q[1] = s; nod[s] = 1;
BFS();
for(int i = 1; i <= n ; ++i){
if(i == s) printf("0 ");
else {
if(d[i] == 0) printf("-1 ");
else printf("%d ", d[i]);
}
}
return 0;
}