Cod sursa(job #2869668)

Utilizator Sebi_MafteiMaftei Sebastioan Sebi_Maftei Data 11 martie 2022 19:00:39
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>
#define MOD 1999999973

using namespace std;

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

int n, m, k, d[100003];
vector<int> h[100003];
bitset<100003> viz;

void Citire()
{
    int x, y;
    fin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        h[x].push_back(y);
    }
}

void BFS(int k)
{
    int nod;
    queue<int> q;
    q.push(k);
    viz[k] = 1;
    while(!q.empty())
    {
        nod = q.front();
        q.pop();
        for (int w : h[nod])
        {
            if(viz[w] == 0)
            {
                viz[nod] = 1;
                d[w] = 1 + d[nod];
                q.push(w);
            }
        }
    }
    for (int i = 1; i <= n; i++)
        if (d[i] == 0 && i != k) fout << -1 << " ";
        else fout << d[i] << " ";
}

int main()
{
    Citire();
    BFS(k);
    return 0;
}