Pagini recente » Cod sursa (job #2182855) | Cod sursa (job #614676) | Cod sursa (job #1954978) | Cod sursa (job #1045325) | Cod sursa (job #2754666)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#define NMAX 100005
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, st ,a[100005];
vector <int > graph[NMAX];
queue <pair < int ,int > > q;
void citire()
{
fin >> n >> m >> st;
for(int i = 1; i <= n; i++)
a[i] = -1;
for(int i = 0; i < m; i++)
{
int x,y;
fin >> x >> y;
graph[x].push_back(y);
}
}
void bfs(){
a[st] = 0;
q.push({st, 0});
while(!q.empty()){
int vf = q.front().first;
int lc = q.front().second + 1;
for(auto &v : graph[vf])
if(a[v] == -1){
q.push({v, lc});
a[v] = lc;
}
q.pop();
}
}
void afisare()
{
for(int i = 1; i <= n; i++)
fout <<a[i] << " ";
}
int main()
{
citire();
bfs();
afisare();
return 0;
}