Pagini recente » Cod sursa (job #1217583) | Cod sursa (job #1219159) | Cod sursa (job #49527) | Cod sursa (job #2643104) | Cod sursa (job #3225047)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, p, i, x, y;
vector<int> a[100002];
int d[100002];
static inline void Parc(int nod = 1) {
queue<int> q;
d[nod] = 1;
q.push(nod);
while(!q.empty()) {
int nod = q.front();
q.pop();
for(auto it : a[nod]) {
if(d[it] == 0) {
d[it] = 1 + d[nod];
q.push(it);
}
}
}
}
int main() {
fin >> n >> m >> p;
for(i = 1; i <= m; i++) {
fin >> x >> y;
a[x].push_back(y);
}
Parc(p);
for(i = 1; i <= n; i++) {
if(d[i] == 0) fout << "-1 ";
else fout << d[i] - 1 << " ";
}
return 0;
}