Pagini recente » Borderou de evaluare (job #1886789) | Cod sursa (job #2779307) | Cod sursa (job #3319643) | Cod sursa (job #3304151) | Cod sursa (job #3334203)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> g[100001];
int n, m, S;
int dist[100001];
int main() {
fin >> n >> m >> S;
for(int i = 1; i <= m; i++) {
int x, y;
fin >> x >> y;
g[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 cur = q.front();
q.pop();
for(int x : g[cur]) {
if(dist[x] == -1) {
dist[x] = dist[cur] + 1;
q.push(x);
}
}
}
for(int i = 1; i <= n; i++)
fout << dist[i] << " ";
return 0;
}