Pagini recente » Cod sursa (job #1601386) | Cod sursa (job #2007081) | Cod sursa (job #493837) | Cod sursa (job #1665130) | Cod sursa (job #2188935)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n, m, target, x, y;
vector <int> v[100005];
queue <int> coada;
int distanta[100005];
void BFS(int nod)
{
coada.push(nod);
distanta[nod] = 1;
while (!coada.empty())
{
int nod = coada.front();
coada.pop();
for (int i = 0; i < v[nod].size(); i++)
if (!distanta[v[nod][i]])
{
coada.push(v[nod][i]);
distanta[v[nod][i]] = distanta[nod] + 1;
}
}
}
int main()
{
f >> n >> m >> target;
while (m--)
{
f >> x >> y;
v[x].push_back(y);
}
BFS(target);
for (int i = 1; i <= n; i++)
g<<distanta[i] - 1 <<" ";
}