Cod sursa(job #3161873)

Utilizator TomaBToma Brihacescu TomaB Data 28 octombrie 2023 09:53:17
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

vector<int> edges[100005];
int dist[100005];


void bfs(int s)
{
    queue<int> q;
    q.push(s);
    
    while (q.empty() == false)
    {
        int x = q.front();
        q.pop();
        for (auto u: edges[x])
        {
            if (dist[u] == 0)
            {
                q.push(u);
                dist[u] = dist[x] + 1;
            }
        }
    }


}

int main()
{
    freopen("bfs.in", "r", stdin);
    freopen("bfs.out", "w", stdout);
    int n, m, s;
    cin >> n >> m >> s;
    while (m--)
    {
        int x, y;
        cin >> x >> y;
        edges[x].push_back(y);
    }

    bfs(s);
    cout<<"ok;;"
    for (int z = 1; z <= n; z++)
    {
        cout << dist[z] << " ";
    }
    return 0;
}