Cod sursa(job #2699499)

Utilizator DavidTosaDavid Tosa DavidTosa Data 24 ianuarie 2021 18:13:50
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
queue<int>cd;
int n,m,x,y,start,i,viz[100010],nra[100010];

struct nod{
    int info;
    nod* urm;
};
nod* v[100010],*q;

int main()
{
    fin>>n>>m>>start;
    for(i=1;i<=m;i++)
    {
        fin>>x>>y;
        q=new nod;
        q->urm=v[x];
        q->info=y;
        v[x]=q;
    }
    viz[start]=1;
    nra[start]=0;
    cd.push(start);
    while(!cd.empty())
    {
        x=cd.front();
        cd.pop();
        q=v[x];
        while(q!=NULL)
        {
            if(viz[q->info]==0)
            {
                nra[q->info]=nra[x]+1;
                viz[q->info]=1;
                cd.push(q->info);
            }
            q=q->urm;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(viz[i]==1)
            fout<<nra[i]<<" ";
        else
            fout<<-1<<" ";
    }
    return 0;
}