Cod sursa(job #3147841)

Utilizator PitigoiOlteanEmanuelPitigoi Oltean Emanuel PitigoiOlteanEmanuel Data 27 august 2023 14:58:15
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;
ifstream cin("bfs.in");
ofstream cout("bfsout");
int n,m,s;

    struct per
    {
       vector <int> dir;

    }v[100001];


vector <int> d;
vector <bool> visited;
int abx(int x)
{
    queue <int > q;
    q.push(x);
    d[x]=1;


    while(!q.empty())
    {

        int cur=q.front();

        q.pop();

        for(int i=1;i<=v[cur].dir[0];i++)
        {
            if(!visited[v[cur].dir[i]])
            {
                visited[v[cur].dir[i]]=1;
                q.push(v[cur].dir[i]);
                d[v[cur].dir[i]]=d[cur]+1;
            }
        }



    }


return 0;
}


int main()
{
    cin>>n>>m>>s;
    for(int i=1;i<=n;i++)
    {
        v[i].dir.push_back(0);
        visited.push_back(0);
        d.push_back(0);
    }
    visited.push_back(0);
    d.push_back(0);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        if(x!=y)
        {
            v[x].dir[0]++;
            v[x].dir.push_back(y);

        }
    }
    visited[s]=1;
    abx(s);
    for(int i=1;i<=n;i++)
    {
        if(d[i]==0)
        {
            cout<<-1<<" ";
            continue;
        }
        cout<<d[i]-1<<" ";
    }



    return 0;
}