Pagini recente » Cod sursa (job #498078) | Cod sursa (job #1830154) | Cod sursa (job #2176617) | Cod sursa (job #1927163) | Cod sursa (job #3230661)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
int d[100001];
int n;
vector<int> v[100001]; // v[i] = lista cu vecinii lui i
void bfs(int x)
{
// pleci din x
queue <int> q;
d[x]=0;
q.push(x);
while(!q.empty())
{
int vf = q.front();
// v[vf]
int k = v[vf].size();
for(int i = 0 ; i < k ; i ++)
{
if(d[ v[vf][i] ]==-1){
d[ v[vf][i] ] = d[vf] + 1;
q.push(v[vf][i]);
}
}
q.pop();
}
}
int main(){
int m, s;
in >> n >> m >> s;
// citire
for (int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y; // x ----> y
v[x].push_back(y);
}
for(int i = 1 ; i <= n ; i ++) d[i] = -1;
bfs(s);
for(int i=1; i<=n; i++)out<<d[i]<<' ';
return 0;
}