Cod sursa(job #2117389)

Utilizator ImbuzanRaduImbuzan Radu ImbuzanRadu Data 28 ianuarie 2018 20:23:41
Problema BFS - Parcurgere in latime Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;
int n, s, m;
int dist[100000], viz[100000];
queue <int> q;
vector <int> a[100000];

void BFS(int start){
    viz[start] = 1;
    q.push(start);
    while(!q.empty()){
        int curent = q.front();
        q.pop();
        for(auto j : a[curent]){
            if(viz[j] == 0){
                dist[j] = dist[curent] + 1;
                q.push(j);
                viz[j] = 1;
            }
        }
    }
}

int main()
{
    ifstream f("bfs.in");
    ofstream g("bfs.out");

    f>>n>>m>>s;

    for(int i = 1; i <= m; i++){
        int x, y;
        f>>x>>y;
        a[x].push_back(y);
    }

    for(int i = 1; i <= n; i++){
        dist[i] = -1;
    }

    dist[s] = 0;
    BFS(s);

    for(int i = 1; i <= n; i++){
        g<<dist[i]<<" ";
    }
    return 0;
}