Pagini recente » Cod sursa (job #195701) | Cod sursa (job #1442060) | Cod sursa (job #2510004) | Cod sursa (job #412126) | Cod sursa (job #3286999)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
vector<int> dist, graph[MAX];
void bfs(int start)
{
queue<int> q;
q.push(start);
dist[start] = 1;
while (!q.empty())
{
int node = q.front();
q.pop();
for (int next : graph[node])
{
if (!dist[next])
{
dist[next] = dist[node] + 1;
q.push(next);
}
}
}
}
int main()
{
int n, m, start;
cin >> n >> m >> start;
dist.assign(n+1, 0);
for (int i = 1; i <= m; ++i)
{
int x, y;
cin >> x >> y;
graph[x].push_back(y);
}
bfs(start);
for (int i = 1; i <= n; ++i)
cout << dist[i]-1 << " ";
return 0;
}