Pagini recente » Cod sursa (job #1508849) | Cod sursa (job #2354833) | Rating Stoica Tudor (Kola) | Cod sursa (job #422699) | Cod sursa (job #2207208)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n,m,k;
struct muchie{int b;muchie *n;}*a[100001],*p;
int d[100001];
void bfs(int x)
{
int L[100003];
L[1]=x;
d[x]=1;
int m=1,M=2;
while(m<M)
{
for(muchie *t=a[L[m]];t!=NULL;t=t->n)if(d[t->b]==0)
{
L[M]=t->b;
d[t->b]=d[L[m]]+1;
++M;
}
++m;
}
}
int main()
{
f>>n>>m;
f>>k;
for(int i=1;i<=m;++i)
{
int A,B;
f>>A>>B;
p=new muchie;
p->b=B;
p->n=a[A];
a[A]=p;
}
bfs(k);
for(int i=1;i<=n;++i)g<<d[i]-1<<' ';
}