Pagini recente » Cod sursa (job #1930055) | Cod sursa (job #1582605) | Cod sursa (job #135760) | Cod sursa (job #139809) | Cod sursa (job #1695831)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
vector <int> a[100001];
queue <int> q;
int n, m, s, i, x, y, sol[100001];
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(); i++)
if (not sol[a[x][i]] and a[x][i] != s)
sol[a[x][i]] = sol[x]+1, q.push(a[x][i]);
}
for (i = 1; i <= n; i++)
(sol[i] == 0 and i != s) ? fo << "-1 " : fo << sol[i] << ' ';
return 0;
}