Pagini recente » Cod sursa (job #1392082) | Cod sursa (job #1102611) | Cod sursa (job #427167) | Cod sursa (job #3153256) | Cod sursa (job #2518834)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s, dist[10000];
vector<int> gr[100000];
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);
}
/*
for (int j = 1; j <= n; j++) {
cout << j << ": ";
for (int i = 0; i < gr[j].size(); i++) {
cout << gr[j][i] << " ";
}
cout << "\n";
}
*/
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++) {
cout << dist[i] << " ";
}
/*
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();
}
*/
return 0;
}