Pagini recente » Cod sursa (job #561456) | Cod sursa (job #762276) | Cod sursa (job #2138744) | Cod sursa (job #2038257) | Cod sursa (job #2422025)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> Muchii[100005];
int cost[1000005];
void BFS(int nod){
queue<int> q;
q.push(nod);
cost[nod] = 0;
while(!q.empty()){
int nd = q.front();
q.pop();
for(int i = 0; i < Muchii[nd].size(); ++i){
if(cost[Muchii[nd][i]] == -1){
cost[Muchii[nd][i]] = cost[nd] + 1;
q.push(Muchii[nd][i]);
}
}
}
}
int main()
{
int n, m, s;
fin >> n >> m >> s;
for(int i = 1; i <= n; ++i)
cost[i] = -1;
for(int i = 1; i <= m; ++i){
int x, y;
fin >> x >> y;
Muchii[x].push_back(y);
}
BFS(s);
for(int i = 1; i <= n; ++i)
fout << cost[i] << ' ';
fout << '\n';
return 0;
}