Pagini recente » Cod sursa (job #1590675) | Cod sursa (job #1630481) | Cod sursa (job #1862571) | Cod sursa (job #41652) | Cod sursa (job #3203400)
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
const char sp = ' ';
const int inf = 0x3f3f3f3f;
const int mod = 666013;
const char out[2][4]{ "NO", "YES" };
#define all(a) a.begin(), a.end()
using ll = long long;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
const int nmax = 5e4;
int n, m, src;
vector<int> g[nmax + 5];
int dist[nmax + 5]{ 0 };
queue<int> q;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
fin >> n >> m >> src;
for (int i = 1; i <= m; ++i) {
int u, v;
fin >> u >> v;
g[u].push_back(v);
}
for (int i = 1; i <= n; ++i) {
dist[i] = inf;
}
dist[src] = 0;
q.push(src);
while (q.size()) {
int u = q.front();
q.pop();
for (auto& v : g[u]) {
if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
for (int i = 1; i <= n; ++i) {
fout << (dist[i] == inf ? -1 : dist[i]) << sp;
}
}