Pagini recente » Cod sursa (job #1061104) | Cod sursa (job #3251519) | Cod sursa (job #129542) | Cod sursa (job #199185) | Cod sursa (job #1394157)
#include <iostream>
#include <fstream>
using namespace std;
int n,a[10000][10000],d[10000];
void parcurge(int s)
{int x,i,in,sf=in=0,q[10000];
d[s]=1;
q[0]=s;
while(in<=sf)
{x=q[in++];
for(i=1;i<=a[x][0];i++)
if(!d[a[x][i]])
{d[a[x][i]]=d[x]+1;
q[++sf]=a[x][i];
}
}
for(i=1;i<=n;i++)d[i]--;
}
int main()
{int i,s,m,v,b;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
fin>>n>>m>>s;
for(i=1;i<=n;i++)
{fin>>v>>b;
a[v][0]++;
a[v][a[v][0]]=b;
a[b][0]++;
a[b][a[b][0]]=v;
}
parcurge(s);
for(i=1;i<=n;i++)
fout<<d[i]<<' ';
}