Cod sursa(job #2935511)

Utilizator _Lucas_Iftinca Lucas _Lucas_ Data 6 noiembrie 2022 19:59:32
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

//ifstream cin("bfs.in");
//ofstream cout("bfs.out");

vector<int> G[100001];
queue<int> q;
int path[100001];
int n, m, s;

int main()
{
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
    }
    for (int i = 1; i <= n; i++)
        path[i] = -1;
    path[s] = 0;
    q.push(s);
    while (!q.empty()) {
        int nod = q.front();
        q.pop();
        for (auto it : G[nod]) {
            if (path[it] == -1) {
                path[it] = path[nod] + 1;
                q.push(it);
            }
        }
    }
    for (int i = 1; i <= n; i++)
        cout << path[i] << ' ';
    return 0;
}