Pagini recente » Cod sursa (job #3272180) | Cod sursa (job #628171) | Cod sursa (job #1826338) | Cod sursa (job #72761) | Cod sursa (job #2388703)
#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[10000], vector_solutii[10000];
vector<int> v[10000];
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()
{
for(int i = 1; i <= n; i++)
vector_solutii[i] = -1;
citire();
coada.push(s);
viz[s] = 1;
vector_solutii[s] = 0;
while(!coada.empty())
{
x = coada.front();
for(int i = 0; i < v[x].size(); i++)
if(!viz[v[x][i]] )
{
viz[v[x][i]] = 1;
vector_solutii[v[x][i]] = vector_solutii[x] + 1;
coada.push(v[x][i]);
}
coada.pop();
}
for(int i = 1; i <= n; i++)
g << vector_solutii[i] << " " ;
}