Cod sursa(job #2366021)

Utilizator qwerty1234qwerty qwerty1234 Data 4 martie 2019 18:05:53
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb

#include <bits/stdc++.h>


using namespace std;

ifstream fin ("bfs.in");
ofstream fout ("bfs.out");

const int Nmax = 100005;

vector < int > L[Nmax];

int n , m , N , d[Nmax];

queue < int > Q;

int main()
{
    int x , y;
    fin >> n >> m >> N;
    for(int i = 1 ; i <= m ; i++)
        fin >> x >> y , L[x].push_back(y);
    Q.push(N);
    while(!Q.empty())
    {
        x = Q.front();
        Q.pop();
        for(auto it : L[x])
            if(!d[it] && it != N)
        {
            Q.push(it);
            d[it] = d[x] + 1;
        }
    }
    for(int i = 1 ; i <= n ; i++)
        if(!d[i] && i != N)
            fout << "-1 ";
    else fout << d[i] << " ";
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}