Pagini recente » Atasamentele paginii Profil cllaude04 | Statistici Gramisteanu Adrian (adria5n) | Autentificare | Statistici Claudiu Epure (cllaude04) | Cod sursa (job #3367519)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, x;
vector<vector<int>> adj;
vector<int> sources;
vector<int> dist;
void BFS(int source) {
queue<int> q;
q.push(source);
dist[source] = 0;
while (!q.empty()) {
int nod = q.front();
q.pop();
for (int i : adj[nod])
if (dist[i] > dist[nod] + 1) {
q.push(i);
dist[i] = dist[nod] + 1;
}
}
for (int i = 1; i <= n; i++) {
if (dist[i] != 2e9)
fout << dist[i] << " ";
else fout << "-1 ";
}
fout << "\n";
}
int main() {
fin >> n >> m >> x;
adj.resize(n + 5);
dist.resize(n + 5);
for (int i = 1; i <= n; i++) {
dist[i] = 2e9;
}
for (int i = 1; i <= m; i++) {
int x, y;
fin >> x >> y;
adj[x].push_back(y);
}
BFS(x);
return 0;
}