Cod sursa(job #2000578)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 14 iulie 2017 10:37:43
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector <int> a[100005];
queue <int> c;
int d[100005];
int main()
{
    int n, m, k;
    in >> n >> m >> k;
    for(int i = 1; i <= m; ++i)
    {
        int x, y;
        in >> x >> y;
        a[x].push_back(y);
    }
    d[k] = 1;
    c.push(k);
    while(!c.empty())
    {
        int x = c.front();
        for(int i = 0; i < a[x].size(); ++i)
            if(d[a[x][i]] == 0)
            {
                d[a[x][i]] = d[x] + 1;
                c.push(a[x][i]);
            }
        c.pop();
    }
    for(int i = 1; i <= n; ++i)
        out << d[i] - 1 << ' ';
    return 0;
}