Cod sursa(job #2650131)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 17 septembrie 2020 15:55:53
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>
#include <vector>
#include <deque>
#define f in
#define g out

using namespace std;
ifstream in ( "bfs.in" );
ofstream out( "bfs.out" );
vector < int > L[100100];
int n, m, x, y, i, s, nod, vecin;
int cost[100100];
deque < int > deq;

int main() {
    
    f>>n>>m>>s;
    for ( i=1; i <= m; i++ ){
        f>>x>>y;
        L[x].push_back(y);
    }
    
    deq.push_back(s); cost[s] = 1;
    while ( !deq.empty() ) {
        nod = deq.front();
        deq.pop_front();
        for ( i=0; i < L[nod].size(); i++ ){
            vecin = L[nod][i];
            if ( !cost[vecin] ){
                cost[vecin] = cost[nod]+1;
                deq.push_back( vecin );
            }
        }
    }
    for ( i=1; i <= n; i++ )
        g<<cost[i]-1<<" ";
    return 0;
}