Cod sursa(job #2678256)

Utilizator NeacsuMihaiNeacsu Mihai NeacsuMihai Data 28 noiembrie 2020 11:24:46
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <fstream>

using namespace std;

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

bool viz[100001];
int d[100001];
struct nod
{
    int info;
    nod * urm;
}*pr[100001];
void BFS(int s)
{
    nod*p=new nod;
    nod*u=p;
    p->info=s;
    p->urm=NULL;
    d[s]=1;
    while(p!=NULL)
    {
        nod*t=pr[p->info];
        while(t!=NULL)
        {
            if(!d[t->info])
            {
                d[t->info]=d[p->info]+1;
                nod*aux=new nod;
                aux->info=t->info;
                aux->urm=NULL;
                u->urm=aux;
                u=aux;
            }
            t=t->urm;
        }
        nod*sterg=p;
        p=p->urm;
        delete sterg;
    }
}
int main()
{
    int n, m, s, i, x, y;
    fin>>n>>m>>s;
    for(i=1; i<=m; i++)
    {
        fin>>x>>y;
        nod*aux1= new nod;
        aux1->info=y;
        aux1->urm=pr[x];
        pr[x]=aux1;

        /*nod*aux2= new nod;
        aux2->info=x;
        aux2->urm=pr[y];
        pr[y]=aux2;*/
    }
    BFS(s);
    for(i=1; i<=n; i++)
    {
        fout<<d[i]-1<<' ';
    }
}