Pagini recente » Cod sursa (job #2698488) | Cod sursa (job #816107) | Cod sursa (job #2099763) | Cod sursa (job #2398254) | Cod sursa (job #2549837)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, nod, D[100001];
vector<int> G[100001];
bool V[100001];
void Bfs()
{
for (int i = 1; i <= n; ++i)
D[i] = -1;
V[nod] = true;
D[nod] = 0;
queue<int> Q;
Q.push(nod);
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int y : G[x])
{
if (!V[y])
{
Q.push(y);
V[y] = true;
D[y] = D[x] + 1;
}
}
}
}
int main()
{
fin >> n >> m >> nod;
for (int i = 1; i <= m; ++i)
{
int x, y;
fin >> x >> y;
G[x].push_back(y);
}
Bfs();
for (int i = 1; i <= n; ++i)
fout << D[i] << " ";
return 0;
}