Pagini recente » Cod sursa (job #3310335) | Cod sursa (job #608608) | Cod sursa (job #3145833) | Cod sursa (job #2947818) | Cod sursa (job #3317182)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 100001;
int n, m, s;
vector<int> adj[MAXN];
int dist[MAXN];
int main() {
ifstream fin("bfs.in");
ofstream fout("bfs.out");
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fin >> n >> m >> s;
for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
adj[x].push_back(y);
}
for (int i = 1; i <= n; ++i) {
dist[i] = -1;
}
queue<int> q;
dist[s] = 0;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
for (int i = 1; i <= n; ++i) {
fout << dist[i] << " ";
}
fout << "\n";
return 0;
}