Pagini recente » Cod sursa (job #478034) | Cod sursa (job #2108153) | Cod sursa (job #361646) | Cod sursa (job #164596) | Cod sursa (job #2649705)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, startnod;
vector <int> muchii[100100];
queue <int> coada;
int distanta[100100];
void bfs()
{
int nod;
while(!coada.empty())
{
nod = coada.front();
coada.pop();
for(unsigned int i = 0; i < muchii[nod].size(); i ++)
{
if(distanta[muchii[nod][i]] == -1)
{
coada.push(muchii[nod][i]);
distanta[muchii[nod][i]] = distanta[nod] + 1;
}
}
}
}
void citire()
{
fin >> n >> m >> startnod;
for(int i = 1; i <= m; i ++)
{
int x, y;
fin >> x >> y;
muchii[x].push_back(y);
}
for(int i = 1; i <= n; i ++)
{
distanta[i] = -1;
}
distanta[startnod] = 0;
coada.push(startnod);
bfs();
for(int i = 1; i <= n; i ++)
fout << distanta[i] << " ";
}
int main()
{
citire();
}