Pagini recente » Cod sursa (job #1687687) | radical-07 | Cod sursa (job #482424) | Istoria paginii runda/vvvvvv/clasament | Cod sursa (job #601672)
Cod sursa(job #601672)
//*
#include<stdio.h>
#include<fstream.h>
#define nmax 10001
ifstream f("bfs.in");
ofstream g("bfs.out");
typedef struct nod{
int nr;
nod *ante;
};
nod *p,*q,*l[nmax];
int a[nmax], n,ic,sc,s,m,i,j, x,c[nmax];
int bf(){
while(ic<=sc){
x=c[ic];
ic++;
q=l[x];
while(q){
if(a[q->nr]==-1){
a[q->nr]=a[x]+1;
sc++;
c[sc]=q->nr;
}
q=q->ante;
}
}
return 0;
}
int main(){
f>>n>>m>>s;
while(f>>i>>j){
p=new nod;
if(i!=j){
p->nr=j;
p->ante=l[i];
l[i]=p;
}
}
c[0]=s;
for(i=1;i<=n;i++) a[i]=-1;
a[s]=0;
bf();
for(i=1;i<=n;i++)
g<<a[i]<<' ';
return 0;
}