Pagini recente » Cod sursa (job #3040814) | Cod sursa (job #1482950) | Cod sursa (job #3194484) | Cod sursa (job #2854556) | Cod sursa (job #864041)
Cod sursa(job #864041)
#include<fstream>
#include<queue>
#include<bitset>
using namespace std;
#define nmax 100001
ifstream fin("bfs.in");
ofstream fout("bfs.out");
queue <int> q;
int a[nmax][1001];
int n,m,s,x,viz[nmax],i;
void citire()
{
int i,x,y,k;
fin>>n>>m>>s;
for (i=1;i<=m;++i)
{
fin>>x>>y;
k=a[x][0];
a[x][k+1]=y;
a[x][0]= k+1;
}
}
void bfs(int z)
{
int i,x,v;
viz[z]=1;
q.push(z);
while (!q.empty())
{
x=q.front(); q.pop();
for (i=1;i<=a[x][0];++i)
{
v=a[x][i];
if (viz[v]==0)
{
q.push(v);
viz[v]=viz[x]+1;
}
}
}
}
int main()
{
citire();
bfs(s);
for (i=1;i<=n;++i)
fout<<viz[i]-1<<' ';
fout<<'\n';
return 0;
}