Cod sursa(job #2587101)

Utilizator k2e0e0w3qDumitrescu Gheorghe k2e0e0w3q Data 22 martie 2020 00:19:33
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
#define N 100001
using namespace std;

array <int, N> dist;
vector <int> G[N];
int main () {
    ifstream fin ("bfs.in");
    ofstream fout ("bfs.out");
    ios::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    int n, m, k, i, j;
    fin >> n >> m >> k;
    for (; m; m--) {
        fin >> i >> j;
        G[i].push_back(j);
    }

    dist.fill(-1);
    queue <int> Q;
    Q.push(k);
    dist[k]=0;
    while (!Q.empty()) {
        auto save=Q.front();
        Q.pop();

        for (auto it: G[save])
            if (dist[it]==-1)
                dist[it]=dist[save]+1,
                Q.push(it);
    }

    for (i=1; i<=n; i++)
        fout << dist[i] << ' ';
    return 0;
}