Pagini recente » Cod sursa (job #3194747) | Cod sursa (job #2904291) | Cod sursa (job #1853576) | Cod sursa (job #2499428) | Cod sursa (job #3246495)
#include <bits/stdc++.h>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
const int NMAX = 1e5;
vector<int> L[NMAX + 1];
int dist[NMAX + 1];
void bfs(int x) {
dist[x] = 0;
queue<int> q;
q.push(x);
while(!q.empty()) {
int node = q.front();
q.pop();
for(auto next : L[node]) {
if(dist[next] == -1) {
dist[next] = dist[node] + 1;
q.push(next);
}
}
}
}
int main() {
int n, m, s;
f >> n >> m >> s;
for(int i = 1; i <= m; i++) {
int x, y;
f >> x >> y;
L[x].push_back(y);
}
for(int i = 1; i <= n; i++) {
dist[i] = -1;
}
bfs(s);
for(int i = 1; i <= n; i++) {
g << dist[i] << ' ';
}
return 0;
}