Pagini recente » Cod sursa (job #1008894) | Cod sursa (job #2803589) | Cod sursa (job #1714533) | Cod sursa (job #2859678) | Cod sursa (job #2856970)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
vector < vector <int> > g;
vector <int> lvl;
queue <int> q;
int n, m, st;
void citire()
{
int x, y;
fin>>n>>m>>st;
g = vector < vector <int> > (n+2);
lvl = vector <int> (n+2);
for (int i=1; i<=m; ++i)
{
fin>>x>>y;
g[x].push_back(y);
//g[y].push_back(x);
}
}
void bfs(int st)
{
q.push(st);
lvl[st]=1;
while (!q.empty())
{
int x=q.front(); q.pop();
for (auto y:g[x])
if (!lvl[y])
{
lvl[y]=lvl[x]+1;
q.push(y);
}
}
}
void afisare()
{
for (int i=1; i<=n; ++i) fout<<lvl[i]-1<<' ';
}
int main()
{
citire();
bfs(st);
afisare();
return 0;
}