Pagini recente » Cod sursa (job #1106842) | Cod sursa (job #2568838) | Cod sursa (job #1387131) | Cod sursa (job #1062207) | Cod sursa (job #307189)
Cod sursa(job #307189)
#include <algorithm>
#define DIM 1000005
using namespace std;
struct nod {int x;
nod *urm;} *lst[DIM];
struct coada {int nod,cost;} c[DIM];
int viz[DIM];
int n,m,s;
void add (int a,int b)
{
nod *p=new nod;
p->x=b;
p->urm=lst[a];
lst[a]=p;
}
int bf (int val)
{
nod *p;
int in,sf;
viz[val]=1;
for (c[in=sf=1].nod=val; in<=sf; )
if (c[in].nod==s)
return c[in].cost;
else
for (p=lst[c[in++].nod]; p; p=p->urm)
if (!viz[p->x])
{
viz[p->x]=1;
c[++sf].nod=p->x;
c[sf].cost=c[in-1].cost+1;
}
return -1;
}
int main ()
{
freopen ("bfs.in","r",stdin);
freopen ("bfs.out","w",stdout);
int i,x,y;
scanf ("%d%d%d",&n,&m,&s);
for (i=1; i<=m; ++i)
{
scanf ("%d%d",&x,&y);
add (y,x);
}
for (i=1; i<=n; ++i)
{
memset (viz,0,DIM);
memset (c,0,DIM);
printf ("%d ",bf (i));
}
return 0;
}