Pagini recente » Cod sursa (job #2779463) | Cod sursa (job #2946916) | Cod sursa (job #418299) | Cod sursa (job #1686379) | Cod sursa (job #642781)
Cod sursa(job #642781)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("bfs.in");
ofstream out ("bfs.out");
int n, m, s, v[100000];
vector <int> a[100000];
queue <int> q;
void citire () {
in >> n >> m >> s;
int x,y;
for (int i = 1; i <= m; ++i) {
in >> x >> y;
a[x].push_back(y);
}
}
void bfs () {
q.push(s);
v[s]=1;
int x, y;
while (!q.empty()) {
x = q.front();
q.pop();
for (size_t i = 0; i < a[x].size(); ++i) {
y = a[x][i];
if (v[y]) {
continue;
}
v[y] = v[x] + 1;
q.push (y);
}
}
}
void afisare () {
for (int i = 1; i <= n; ++i) {
if (i == s) {
out << "0 ";
continue;
}
out << v[i] - 1 << ' ';
}
out << '\n';
}
int main () {
citire ();
bfs ();
afisare ();
return 0;
}