Pagini recente » Cod sursa (job #2159044) | Cod sursa (job #2772165) | Cod sursa (job #3192540) | Cod sursa (job #2894) | Cod sursa (job #239728)
Cod sursa(job #239728)
#include<stdio.h>
#define N 10000
#define M 100000
int n,m,s;
int* a[N],d[N];
int dist[M];
void citire()
{
int x,y,i;
scanf("%d%d%d",&n,&m,&s);
while(m--)//citesc pt a calcula gradele
{
scanf("%d%d",&x,&y);
++d[x];
}
for(i=1;i<=n;++i)//alocarea dinamica
{
a[i]=new int[d[i]+1];
a[i][0]=0;
}
fclose(stdin);
freopen("bfs.in","r",stdin);
scanf("%d%d%d",&n,&m,&s);
while(m--)
{
scanf("%d%d",&x,&y);
a[x][++a[x][0]]=y;//il adaug pe y in lista vecinilor lui x
}
}
void proba()
{
int i,j;
for(i=1;i<=n;++i)
{
printf("%d: ",i);
for(j=1;j<=a[i][0];++j)
printf("%d ",a[i][j]);
printf("\n");
}
}
void bfs(){
int p=0,u=0,caut[N],coada[N];
int x,y,i;
coada[u++]=s;//adaug
dist[s]=1;
while(p<u)
{
x=coada[p++];//scot
for(i=1;i<=a[x][0];++i)
{
y=a[x][i];
if(!dist[y])
{
coada[u++]=y;
dist[y]=dist[x]+1;
}
}
}
}
void afisare(){
int i;
for(i=1;i<=n;++i)
printf("%d ",dist[i]-1);
}
int main()
{
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
citire();
bfs();
afisare();
fclose(stdin);
fclose(stdout);
return 0;
}