Pagini recente » Cod sursa (job #1891963) | Cod sursa (job #2969823) | Cod sursa (job #3148438) | Cod sursa (job #2570785) | Cod sursa (job #3165123)
#include <fstream>
#include <vector>
#include <queue>
std::ifstream fin("bfs.in");
std::ofstream fout("bfs.out");
const int nMax = 1e5;
std::vector<std::vector<int>> graf;
std::queue<int> q;
int cost[nMax];
void bfs(int nod) {
q.push(nod);
cost[nod] = 1;
while(!q.empty()) {
nod = q.front();
q.pop();
for(int next : graf[nod])
if(cost[next] == 0) {
q.push(next);
cost[next] = cost[nod] + 1;
}
}
}
int main() {
int n, m, k;
fin >> n >> m >> k;
k--;
graf.assign(n, std::vector<int>());
for(int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
x--, y--;
graf[x].push_back(y);
}
bfs(k);
for(int i = 0; i < n; ++i)
fout << cost[i] - 1 << ' ';
graf.clear();
fin.close();
fout.close();
return 0;
}