Pagini recente » Borderou de evaluare (job #3288476) | Borderou de evaluare (job #2268537) | Borderou de evaluare (job #3367087) | Istoria paginii utilizator/ehsanp | Cod sursa (job #3367076)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> g[100005];
int dist[100005];
void bfs(int s) {
queue<int> q;
q.push(s);
dist[s] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int y : g[x]) {
if (dist[y] == -1) {
dist[y] = dist[x] + 1;
q.push(y);
}
}
}
}
int main() {
int n, m, s;
fin >> n >> m >> s;
for (int i = 0; i < m; i++) {
int x, y;
fin >> x >> y;
g[x].push_back(y);
}
for (int i = 1; i <= n; i++)
dist[i] = -1;
bfs(s);
for (int i = 1; i <= n; i++)
fout << dist[i] << ' ';
}