Pagini recente » Borderou de evaluare (job #1796328) | Cod sursa (job #62412) | Borderou de evaluare (job #2487831) | Borderou de evaluare (job #307461) | Cod sursa (job #2329309)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
#define limn 100010
int n,m,s;
vector <int> v[limn];
int path[limn];
void bfs(int start)
{
queue <int> q;
int nod;
path[start] = 0;
q.push(start);
while (!q.empty())
{
nod = q.front();
q.pop();
for(auto it:v[nod])
if (path[it] == -1)
{
path[it] = path[nod] + 1;
q.push(it);
}
}
}
int main()
{
int x,y;
fin>>n>>m>>s;
while(m--)
{
fin>>x>>y;
v[x].push_back(y);
}
for(int i=1; i<=n; i++) path[i] = -1;
bfs(s);
for(int i=1; i<=n; i++) fout<<path[i]<<' ';
fin.close();
fout.close();
return 0;
}