Pagini recente » Cod sursa (job #2854912) | Cod sursa (job #1271794) | Cod sursa (job #2377941) | Cod sursa (job #2154557) | Cod sursa (job #1377033)
#include <bits/stdc++.h>
using namespace std ;
const int NMAX = 100005 ;
const int INF = 0x3f3f3f3f ;
ifstream fin("bfs.in") ;
ofstream fout("bfs.out") ;
vector <int> V[NMAX] ;
queue <int> Q ;
int S, N, M, cost[NMAX];
int cnt = 0 ;
void BFS(int nod)
{
Q.push(nod) ;
cost[nod] = 0 ;
while(!Q.empty())
{
nod = Q.front() ;
Q.pop() ;
for(int i = 0 ; i < V[nod].size() ; ++ i)
if(cost[V[nod][i]] == -1)
{
cost[V[nod][i]] = cost[nod] + 1 ;
Q.push(V[nod][i]) ;
}
}
}
int main()
{
fin >> N >> M >> S ;
for(int i = 1 ; i <= M ; ++ i)
{
int XX, YY ;
fin >> XX >> YY ;
V[XX].push_back(YY) ;
}
memset(cost, -1, sizeof cost) ;
BFS(S) ;
for(int i = 1 ; i <= N ; ++ i)
fout << cost[i] << ' ' ;
fin.close() ;
fout.close() ;
return 0 ;
}