Cod sursa(job #2498443)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 23 noiembrie 2019 22:00:46
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define f in
#define g out

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

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