Pagini recente » Cod sursa (job #944754) | Cod sursa (job #2327353) | Cod sursa (job #937070) | Cod sursa (job #611170) | Cod sursa (job #1025765)
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct nod
{
int vf;
nod *urm;
};
nod *l[100010];
int n,m,st,viz[100010],nr,pas[100010];
void adaug(nod *&pr, int x)
{
nod *nou;
nou=new nod;
nou->vf=x;
nou->urm=pr;
pr=nou;
}
void citire()
{
int i,x,y;
scanf("%d%d%d",&n,&m,&st);
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
adaug(l[x],y);
}
}
void bfs(int fin)
{
queue<int> q;
q.push(st);
int p,v;
nod *c;
while(!q.empty() && !viz[fin])
{
p=q.front();
q.pop();
viz[p]=1;
c=l[p];
while(c)
{
if(!viz[c->vf])
{
q.push(c->vf);
pas[c->vf]=pas[p]+1;
}
c=c->urm;
}
}
if(viz[fin])
printf("%d ",pas[fin]);
else printf("-1 ");
}
int main()
{
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
citire();
for(int i=1;i<=n;i++)
{
if(i==st)
printf("0 ");
else
{
bfs(i);
nr=0;
memset(viz,0,sizeof(viz));
memset(pas,0,sizeof(pas));
}
}
return 0;
}