Pagini recente » Cod sursa (job #1182171) | Cod sursa (job #1374544) | Cod sursa (job #685019) | Cod sursa (job #966678) | Cod sursa (job #1695812)
#include <fstream>
#include <queue>
using namespace std;
struct arc {
queue<int>v;
}a[1000001];
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].v.push(y);
q.push(s);
while (!q.empty()) {
x = q.front(); q.pop();
while (!a[x].v.empty()) {
y = a[x].v.front(); a[x].v.pop();
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;
}