Pagini recente » Cod sursa (job #1180447) | Cod sursa (job #1603799) | Cod sursa (job #3246083) | Cod sursa (job #1330746) | Cod sursa (job #3140085)
#include <bits/stdc++.h>
#define for_auto(v) for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, p, i, x, y;
int d[100002];
vector<int> a[100002];
static inline void Lee(int x) {
queue<int> q;
d[x] = 1;
q.push(x);
while(!q.empty()) {
int curr = q.front();
for_auto(a[curr]) {
if(!d[*it]) {
d[*it] = d[curr] + 1;
q.push(*it);
}
}
q.pop();
}
}
int main() {
fin >> n >> m >> p;
for(i = 1; i <= m; i++) {
fin >> x >> y;
a[x].push_back(y);
}
Lee(p);
for(i = 1; i <= n; i++) {
if(d[i] == 0) fout << "-1 ";
else fout << d[i] - 1 << " ";
}
return 0;
}