Cod sursa(job #1751109)

Utilizator FredyLup Lucia Fredy Data 31 august 2016 18:52:17
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

#define lim 100001
vector <int> G[lim];
int path[lim];
queue <int> q;
int S,N,M;


void bfs()
{
    int act;
    q.push(S);
    path[S]=0;
    act=S;

    while(!q.empty())
    {
        act=q.front();
        q.pop();

        for(auto it:G[act])
            if(path[it]==-1)
            {
                path[it]=path[act]+1;
                q.push(it);
            }
    }

}



int main()
{
    int i,x,y;

    fin>>N>>M>>S;

    for(i=1; i<=M; i++)
    {
        fin>>x>>y;
        G[x].push_back(y);
    }

    for(i=1; i<=N; i++)
        path[i]=-1;

    bfs();

    for(i=1; i<=N; i++)
        fout<<path[i]<<' ';

    fin.close();
    fout.close();
    return 0;
}