Pagini recente » Cod sursa (job #3201896) | Cod sursa (job #1025185) | Cod sursa (job #1102328) | Cod sursa (job #2067200) | Cod sursa (job #2764378)
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxVert = 1000001;
vector<int> Adj[maxVert];
queue<int> nodes;
int dist[maxVert];
int main() {
int n, m, s, x, y;
cin >> n >> m >> s;
for (int i = 1; i <= m; ++i) {
cin >> x >> y;
Adj[x].push_back(y);
}
nodes.push(s);
int front;
for (int i = 1; i <= n; ++i) {
dist[i] = -1;
}
dist[s] = 0;
while (!nodes.empty()) {
front = nodes.front();
nodes.pop();
int neighbours = Adj[front].size();
for (int i = 0; i < neighbours; ++i) {
if (dist[Adj[front][i]] == -1) {
dist[Adj[front][i]] = dist[front] + 1;
nodes.push(Adj[front][i]);
}
}
}
for (int i = 1; i <= n; ++i) {
cout << dist[i] << " ";
}
cout << "\n";
return 0;
}