Cod sursa(job #1750548)

Utilizator FredyLup Lucia Fredy Data 30 august 2016 14:28:58
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

#define lim 100001

int N,M,S,x,y;

vector<int> G[lim];
int path[lim];
queue <int> q;


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

    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()
{
    fin>>N>>M>>S;

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

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

    bfs();

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


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