Pagini recente » Cod sursa (job #3328766) | Cod sursa (job #1365913) | Cod sursa (job #926760) | Cod sursa (job #255927) | Cod sursa (job #3344395)
#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()
{
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, s;
fin >> n >> m >> s;
dist.assign(n+1, 0);
for (int i = 1; i <= m; ++i)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
bfs(s);
for (int i = 1; i <= n; ++i)
fout << dist[i]-1 << " ";
return 0;
}