Pagini recente » Cod sursa (job #465683) | Cod sursa (job #305308) | Cod sursa (job #1942727) | Cod sursa (job #608877) | Cod sursa (job #2587101)
#include <bits/stdc++.h>
#define N 100001
using namespace std;
array <int, N> dist;
vector <int> G[N];
int main () {
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
ios::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
int n, m, k, i, j;
fin >> n >> m >> k;
for (; m; m--) {
fin >> i >> j;
G[i].push_back(j);
}
dist.fill(-1);
queue <int> Q;
Q.push(k);
dist[k]=0;
while (!Q.empty()) {
auto save=Q.front();
Q.pop();
for (auto it: G[save])
if (dist[it]==-1)
dist[it]=dist[save]+1,
Q.push(it);
}
for (i=1; i<=n; i++)
fout << dist[i] << ' ';
return 0;
}