Pagini recente » Cod sursa (job #1949255) | Cod sursa (job #706560) | Cod sursa (job #406648) | Cod sursa (job #322166) | Cod sursa (job #2545912)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s, dist[100005];
vector<int> gr[100005];
queue <int> q;
int main() {
fin >> n >> m >> s;
int x;
for (int i = 0; i < m; i++) {
int a, b;
fin >> a >> b;
gr[a].push_back(b);
}
q.push(s);
for (int i = 0; i <= n; i++) {
dist[i] = -1;
}
dist[s] = 0;
while (!q.empty()) {
int nod = q.front();
for (int j = 0; j < gr[nod].size(); j++) {
if (dist[gr[nod][j]] == -1) {
q.push(gr[nod][j]);
dist[gr[nod][j]] = dist[nod] + 1;
}
}
q.pop();
}
for (int i = 1; i <= n; i++) {
fout << dist[i] << " ";
}
return 0;
}
// queue
/*
cout << "Vecini 2:\n";
for (int i = 0; i < gr[s].size(); i++) {
q.push(gr[s][i]);
cout << gr[s][i] << " ";
}
*/
/*
while(!q.empty()){
cout<<" "<<q.front();
q.pop();
}
*/