Pagini recente » Cod sursa (job #1500530) | Cod sursa (job #2728546) | Cod sursa (job #130152) | Istoria paginii utilizator/bozoncanca01 | Cod sursa (job #1183321)
#include<fstream>
#include<vector>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
const int nmax = 100009;
vector<int> v[nmax];
int coada[nmax],cost[nmax],lg[nmax];
void bfs(int S)
{
int i,j;
for(i = 1 ; i <= 100000 ; i++)
cost[i] = -1;
int L = 1;
coada[L] = S;
cost[S] = 0;
for(i = 1 ; i <= L ; i++)
for(j = 0 ; j < lg[coada[i]]; j++)
if(cost[v[coada[i]][j]] == -1)
{
coada[++L] = v[coada[i]][j];
cost[coada[L]] = cost[coada[i]]+1;
}
}
int main()
{
int N,M,S,i,x,y;
in>>N>>M>>S;
for(i = 1 ; i <= M ; i++)
{
in>>x>>y;
v[x].push_back(y);
}
for(i = 1 ; i <= N ; i++)
lg[i] = v[i].size();
bfs(S);
for(i = 1 ; i <= N ; i++)
out<<cost[i]<<" ";
return 0;
}