Pagini recente » Cod sursa (job #1896018) | Cod sursa (job #1634628) | Cod sursa (job #784359) | Cod sursa (job #806572) | Cod sursa (job #3295168)
//Lookin' like a true survivor,
//Feelin' like a little kid
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s, x, y, v[100005], a[10000][10000];
void bfs(int start)
{
queue<int>q;
for(int i=1;i<=n;i++)
v[i]=-1;
v[start]=0;
q.push(start);
while(!q.empty())
{
int nod_curent=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(v[i]==-1 && a[nod_curent][i]==1)
{
q.push(i);
v[i]=v[nod_curent]+1;
}
}
}
}
int main()
{
fin>>n>>m>>s;
for(int i=1;i<=m;i++)
{
fin>>x>>y;
a[x][y]=1;
}
bfs(s);
for(int i=1;i<=n;i++)
fout<<v[i]<<' ';
return 0;
}
//And I'm still standin' after all this time