Pagini recente » Cod sursa (job #1546787) | Cod sursa (job #1385848) | Cod sursa (job #566100) | Cod sursa (job #1065161) | Cod sursa (job #1497287)
#include <iostream>
#include <fstream>
#include <stdlib.h>
#define nmax 100000
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
long *g[nmax] , viz[nmax];
void bfs(long x)
{long i,y,in,sf=in=0;
long q[nmax];
q[in]=x;viz[x]=1;
while(in<=sf)
{y=q[in++];
for(i=1;i<=g[y][0];i++)
if(!viz[g[y][i]])
{viz[g[y][i]]=viz[y]+1;
q[++sf]=g[y][i];
}
}
}
int main()
{long n,m,s,i,x,y;
fin>>n>>m>>s;
for(i=1;i<=n;i++)
{ g[i]=(long *)realloc(g[i],sizeof(long));
g[i][0]=0;
}
for(i=1;i<=m;i++)
{
fin>>x>>y;
g[x][0]++;
g[x]=(long*)realloc(g[x],(g[x][0]+1)*sizeof(long));
g[x][g[x][0]]=y;
}
bfs(s);
for(i=1;i<=n;i++)fout<<(viz[i]-1)<<' ';
}