Pagini recente » Cod sursa (job #1271270) | Cod sursa (job #1986184) | Cod sursa (job #1129992) | Cod sursa (job #1197369) | Cod sursa (job #2054475)
#include <stdio.h>
#include <stdlib.h>
#define NMax 100001
struct llist{
int info;
struct llist *next;
};
int main()
{
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
struct llist *first[NMax], *last[NMax];
int len[NMax] = {0}, viz[NMax] = {0};
int n = 0, m = 0, s = 0, x = 0, y = 0;
scanf("%d%d%d",&n,&m,&s);
for(int i = 0; i < m; ++i){
scanf("%d%d",&x,&y);
struct llist *p;
p = malloc(sizeof(struct llist));
p->info = y;
p->next = NULL;
if(len[x] == 0){
first[x] = p;
last[x] = first[x];
len[x] ++;
}else{
last[x]->next = p;
last[x] = last[x]->next;
}
}
int qfirst = 0, qlast = 0, q[NMax];
viz[s] = 1;
q[qfirst] = s;
while(qfirst <= qlast){
int node = q[qfirst];
qfirst++;
for(struct llist *p = first[node]; p != NULL; p = p->next){
if(!viz[p->info]){
viz[p->info] = viz[node] + 1;
q[++qlast] = p->info;
}
}
}
for(int i = 1; i <= n; ++i){
if(!viz[i])
printf("-1 ");
else
printf("%d ",viz[i] - 1);
}
return 0;
}