Pagini recente » Cod sursa (job #2549264) | Cod sursa (job #996707) | Cod sursa (job #1022588) | Cod sursa (job #2872709) | Cod sursa (job #3213695)
#include <bits/stdc++.h>
#define MOD 1999999973
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, S;
int d[100005];
vector<int> a[100005];
queue<int> q;
void BFS(int x)
{
for (int i = 1; i <= n; i++)
d[i] = 2e9;
d[x] = 0;
q.push(x);
while (!q.empty())
{
x = q.front();
q.pop();
for (int e : a[x])
if (d[e] > d[x] + 1)
{
d[e] = d[x] + 1;
q.push(e);
}
}
}
int main()
{
int x, y;
fin >> n >> m >> S;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
a[x].push_back(y);
}
BFS(S);
for (int i = 1; i <= n; i++)
if (d[i] != 2e9) fout << d[i] << " ";
else fout << "-1 ";
fout << "\n";
}