Cod sursa(job #2668200)

Utilizator bogdan.gusuleacGusuleac Bogdan bogdan.gusuleac Data 4 noiembrie 2020 17:15:58
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include<fstream>
#include<vector>
#include<queue>

using namespace std;

#define MAXVALUE 100010

ifstream f("bfs.in");
ofstream g("bfs.out");

vector <int>coresp[MAXVALUE];
int dist[MAXVALUE];
queue <int>q;

void bfs()
{
    int nod, i, next;

    while(q.empty()==0)
    {
        nod=q.front();
        q.pop();

        for(i=0; i < coresp[nod].size(); i++)
        {
            next = coresp[nod][i];
            if(dist[next] == -1)
            {
                q.push(next);
                dist[next] = dist[nod] + 1;
            }
        }
    }
}

int main()
{
    int numNodes, lines, src;
    f>>numNodes>>lines>>src;

    for(int i=1; i < lines; i++)
    {
        int x, y;
        f>>x>>y;
        coresp[x].push_back(y);
    }

    for(int i=1; i <= numNodes; i++)
        dist[i] = -1;

    dist[src]=0;
    q.push(src);
    bfs();

    for(int i=1; i <= numNodes; i++)
        g<<dist[i]<<" ";
}