Pagini recente » Cod sursa (job #1906317) | Cod sursa (job #1201091) | Cod sursa (job #136582) | Cod sursa (job #2145152) | Cod sursa (job #1695821)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
vector <int> a[100001];
int n, m, s, i, sol[100001], x, y;
queue <int> q;
int main () {
ifstream fi("bfs.in");
ofstream fo("bfs.out");
fi >> n >> m >> s;
for (i = 1; i <= m; i++)
fi >> x >> y, a[x].push_back(y);
q.push(s);
while (!q.empty()) {
x = q.front(); q.pop();
for (i = 0; i <= a[x].size()-1; i++) {
y = a[x][i];
if (not sol[y] and y != s)
sol[y] = sol[x]+1, q.push(y);
}
}
for (i = 1; i <= n; i++)
sol[i] == 0 and i != s ? fo << "-1 " : fo << sol[i] << ' ';
return 0;
}