Pagini recente » Cod sursa (job #2713604) | Cod sursa (job #1920494) | Cod sursa (job #287992) | Cod sursa (job #2747191) | Cod sursa (job #2517409)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s, a, b;
bool f[100100];
int d[100100];
queue<pair<int, int> > c;
struct str {
vector<int> v;
} x[100100];
int main() {
fin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
fin >> a >> b;
x[a].v.push_back(b);
}
for (int i = 1; i <= n; i++)
d[i] = -1;
c.push(make_pair(s, 0));
f[s] = true;
while (!c.empty()) {
a = c.front().first;
int cost = c.front().second;
d[a] = cost;
int sf = x[a].v.size();
for (int i = 0; i < sf; i++) {
b = x[a].v[i];
if (!f[b]) {
f[b] = true;
c.push(make_pair(b, cost + 1));
}
}
c.pop();
}
for (int i = 1; i <= n; i++)
fout << d[i] << ' ';
return 0;
}