Mai intai trebuie sa te autentifici.
Cod sursa(job #3317167)
| Utilizator | Data | 22 octombrie 2025 16:45:23 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 20 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.83 kb |
#include <bits/stdc++.h>
using namespace std;
vector<int> G[100005];
bool viz[100005];
vector<int> dist(100005, -1);
void BFS(int v) {
queue<int> Q;
Q.push(v);
dist[v] = 0;
while (!Q.empty()) {
int c = Q.front();
viz[c] = true;
for (int n : G[c]) {
if (!viz[n]) {
Q.push(n);
dist[n] = dist[c] + 1;
}
if (dist[n] > dist[c] + 1) {
dist[n] = dist[c] + 1;
}
}
Q.pop();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("bfs.in", "r", stdin);
freopen("bfs.out", "w", stdout);
int n, m, s;
cin >> n >> m >> s;
for (int i = 1; i <= m; ++i) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
}
BFS(s);
for (int i = 1; i <= n; ++i) {
cout << dist[i] << ' ';
}
return 0;
}