Pagini recente » Cod sursa (job #2728750) | Cod sursa (job #2632606) | Istoria paginii runda/rar39 | Istoria paginii runda/795353277152115565 | Cod sursa (job #2632509)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int N, M, S;
vector<vector<int>> gr;
vector<int> t;
void read() {
int x, y;
fin >> N >> M >> S;
gr.resize(N + 1);
t.resize(N + 1, 0);
for (int i = 0; i < M; ++i) {
fin >> x >> y;
gr[x].push_back(y);
}
}
void BFS(int node) {
queue<int> q;
q.push(node);
int nod;
while (!q.empty()) {
nod = q.front();
q.pop();
for (auto i : gr[nod]) {
if (t[i] == 0 && i != node) {
q.push(i);
t[i] = t[nod] + 1;
}
}
}
}
int main()
{
read();
BFS(S);
for (int i = 1; i <= N; ++i) {
if (t[i] == 0 && i != S) {
fout << -1 << " ";
}
else
fout << t[i] << " ";
}
return 0;
}