Pagini recente » Cod sursa (job #1989206) | Cod sursa (job #2843451) | Cod sursa (job #2081594) | Cod sursa (job #1889842) | Cod sursa (job #2421393)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n, m, s, x, y, viz[100003], vector_solutii[100003];
vector<int> v[100003];
queue<int> coada;
void citire()
{
f >> n >> m >> s;
for(int i = 1; i <= m; i++)
{
f >> x >> y;
v[x].push_back(y);
}
/*for(int i = 1; i <= n; i++)
vector_solutii[i] = -1;*/
}
int main()
{
citire();
coada.push(s);
viz[s] = 1;
vector_solutii[s] = 1;
while(!coada.empty())
{
x = coada.front();
coada.pop();
for(int i = 0; i < v[x].size(); i++)
if(!viz[v[x][i]] )
{
coada.push(v[x][i]);
viz[v[x][i]] = 1;
vector_solutii[v[x][i]] = vector_solutii[x] + 1;
}
//coada.pop();
}
for(int i = 1; i <= n; i++)
g << vector_solutii[i] - 1 << " " ;
}