Pagini recente » Cod sursa (job #551047) | Cod sursa (job #888014) | Cod sursa (job #1018827) | Cod sursa (job #1744809) | Cod sursa (job #1501093)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>
using namespace std ;
const int NMAX = 100005 ;
ifstream fin("bfs.in") ;
ofstream fout("bfs.out") ;
int N, M, S, cost[NMAX];
vector <int> V[NMAX] ;
queue <int> Q ;
void BFS(int nod)
{
Q.push(nod) ;
cost[nod] = 0 ;
while(!Q.empty())
{
int nod_X = Q.front() ;
Q.pop() ;
for(int i = 0 ; i < V[nod_X].size() ; ++ i)
if(cost[V[nod_X][i]] == -1)
{
cost[V[nod_X][i]] = cost[nod_X] + 1 ;
Q.push(V[nod_X][i]) ;
}
}
}
int main()
{
fin >> N >> M >> S;
for(int i = 1 ; i <= M ; ++ i)
{
int x, y ;
fin >> x >> y ;
V[x].push_back(y) ;
}
memset(cost, -1, sizeof cost) ;
BFS(S) ;
for(int i = 1 ; i <= N ; ++ i )
fout << cost[i] << ' ' ;
fin.close() ;
fout.close() ;
return 0 ;
}