Pagini recente » Cod sursa (job #2841571) | Cod sursa (job #1694658) | Cod sursa (job #132879) | Cod sursa (job #587792) | Cod sursa (job #2649714)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, startnod;
vector <int> muchii[100100];
int coada[100100];
int st[100100];
int distanta[100100];
void bfs()
{
int nod, left = 1, right = 1;
while(left <= right)
{
nod = coada[left];
left++;
for(unsigned int i = 0; i < muchii[nod].size(); i ++)
{
if(distanta[muchii[nod][i]] == -1)
{
right++;
coada[right] = 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[1] = startnod;
bfs();
for(int i = 1; i <= n; i ++)
fout << distanta[i] << " ";
}
int main()
{
citire();
}