Pagini recente » Cod sursa (job #1499490) | Cod sursa (job #278500)
Cod sursa(job #278500)
#include<stdio.h>
#define in "bfs.in"
#define out "bfs.out"
struct nod{
long nd;
nod *next;
};
nod *p,*l[100001];
long n,m,st,s[100001],i,x,y,bf[100001],cost[100001];
int main()
{
freopen(in,"r",stdin);
freopen(out,"w",stdout);
scanf("%ld%ld%ld",&n,&m,&st);
for(i=1;i<=m;i++)
{
scanf("%ld%ld",&x,&y);
p=new nod;
p->nd=y;
p->next=l[x];
l[x]=p;
}
x=y=1;
s[st]=1;
bf[x]=st;
cost[st]=0;
while(x<=y)
{
p=l[bf[x]];
while(p)
{
if(!s[p->nd])
{
bf[++y]=p->nd;
s[p->nd]=1;
cost[p->nd]=cost[bf[x]]+1;
}
p=p->next;
}
x++;
}
for(i=1;i<=n;i++)
if(!cost[i] && i!=st)
printf("-1 ");
else
printf("%d ",cost[i]);
return 0;
}