Pagini recente » Profil now_or_never | Cod sursa (job #2246031) | Cod sursa (job #672674) | Cod sursa (job #2648843) | Cod sursa (job #2952648)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
vector<int> v[10001];
queue<int> q;
int n, m, s;
int dist[10001];
void citire()
{
fin >> n >> m >> s;
int x, y;
for(int i = 0; i < m; ++i)
{
fin >> x >> y;
v[x].push_back(y);
}
for(int i = 1; i <= n; ++i)
dist[i] = -1;
}
void bsf(int s)
{
q.push(s);
dist[s] = 0;
while(!q.empty())
{
int el = q.front();
q.pop();
for(int i = 0; i < v[el].size(); ++i)
if(dist[v[el][i]] == -1)
{
dist[v[el][i]] = dist[el] + 1;
q.push(v[el][i]);
}
}
}
void rezolvare()
{
bsf(s);
for(int i = 1; i <= n; ++i)
fout << dist[i] << ' ';
}
int main()
{
citire();
rezolvare();
return 0;
}