Pagini recente » Cod sursa (job #2453745) | Cod sursa (job #2287289) | Cod sursa (job #2574008) | Cod sursa (job #1967092) | Cod sursa (job #2400963)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
const int N = 100001;
vector <int> v[N];
queue <int> q;
int n, m, s, d[N];
void bfs (int nod) {
q.push(nod);
while(!q.empty()) {
int k = q.front();
q.pop();
for(int i = 0; i < v[k].size(); i ++) {
int t = v[k][i];
if(d[t] == 0 && t != nod) {
d[t] = d[k] + 1;
q.push(t);
}
}
}
}
int main()
{
f >> n >> m >> s;
for(int i = 1; i <= m; i ++) {
int x, y;
f >> x >> y;
v[x].push_back(y);
}
bfs (s);
for(int i = 1; i <= n; i ++) {
if(d[i] == 0 && i != s)
g << -1 << ' ';
else
g << d[i] << ' ';
}
return 0;
}