Cod sursa(job #947416)

Utilizator BitOneSAlexandru BitOne Data 7 mai 2013 13:59:32
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <queue>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>

using namespace std;

const int NMAX = 100011;

int d[NMAX];
queue<int> Q;
vector<int> G[NMAX];

int main()
{
    int N, M, start, x, y;
    ifstream in("bfs.in");
    ofstream out("bfs.out");

    for(in >> N >> M >> start; M; --M)
    {
        in >> x >> y;
        G[x].push_back(y);
    }
    fill(d, d + N + 1, -1);
    d[start] = 0;
    for(Q.push(start); !Q.empty(); Q.pop())
    {
        x = Q.front();
        for(auto& y : G[x])
        {
            if(-1 == d[y])
            {
                d[y] = d[x] + 1;
                Q.push(y);
            }
        }
    }
    copy(d + 1, d + N + 1, ostream_iterator<int>(out, " "));
    out << '\n';
}