Cod sursa(job #2578616)

Utilizator levladiatorDragutoiu Vlad-Ioan levladiator Data 11 martie 2020 12:41:30
Problema BFS - Parcurgere in latime Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
#define NMAX 100005
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");

vector < int > v[NMAX];
int n,m,s,a,b,rasp[NMAX];
bool verif[NMAX];

void bfs(int nod)
{
    int z=rasp[nod];
    int lungime=v[nod].size();
    for(int i=0;i<lungime;i++)
    {
        if(verif[v[nod][i]]==0)
        {
            verif[v[nod][i]]=1;
            rasp[v[nod][i]]=z+1;
            bfs(v[nod][i]);
        }
    }
}

int main()
{
    fin>>n>>m>>s;
    for(int i=1;i<=m;i++)
    {
        fin>>a>>b;
        if(a!=b)v[a].push_back(b);
    }
    verif[s]=1;
    rasp[s]=0;
    bfs(s);
    for(int i=1;i<=n;i++)
    {
        if(rasp[i]==0&&i!=s)rasp[i]=-1;
        fout<<rasp[i]<<" ";
    }
}