Pagini recente » Cod sursa (job #704534) | Cod sursa (job #907407) | Cod sursa (job #400176) | Cod sursa (job #3167335) | Cod sursa (job #384878)
Cod sursa(job #384878)
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
struct nod{
int info;
nod *next;
};
nod *a[100005];
int d[100005],n,m,s;
void citire()
{
freopen("bfs.in","r",stdin);
scanf("%d%d%d", &n, &m, &s);
int i,j;
nod *p;
for(i=1;i<=n;i++)
a[i]=NULL,d[i]=-1;
while(scanf("%d%d", &i, &j)!=EOF)
{
p=new nod;
p->info=j;
p->next=a[i];
a[i]=p;
}
}
void bfs()
{
int k;
nod *st,*dr;
st=dr=new nod;
st->info=s;
st->next=NULL;
dr=st;
d[s]=0;
while(st)
{
k=st->info;
for(nod *p=a[k];p;p=p->next)
{
if(d[p->info]==-1)
{
nod *q=new nod;
q->info=p->info;
q->next=NULL;
dr->next=q;
dr=q;
d[p->info]=d[k]+1;
}
}
nod *t=st;
st=st->next;
delete t;
}
}
void write()
{
freopen("bfs.out","w",stdout);
int i;
for(i=1;i<=n;i++)
printf("%d ",d[i]);
}
int main()
{
citire();
bfs();
write();
return 0;
}