Cod sursa(job #2682299)

Utilizator teofilotopeniTeofil teofilotopeni Data 8 decembrie 2020 13:41:55
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;

//  BFS

struct Node {
    int minim = -1;
    deque<int> from;
};

int n, m, s;
deque <Node> nodes;

void Recurs(int i) {
    for (int j = 0; j < nodes[i].from.size(); j++) {
        int index = nodes[i].from[j];
        if (nodes[index].minim > nodes[i].minim + 1 || nodes[index].minim == -1)
            nodes[index].minim = nodes[i].minim + 1;
        if (index != s)
            Recurs(index);
    }
}

int main() {
    freopen("FileIn.txt", "r", stdin);
    int i, x, y;
    cin >> n >> m >> s;
    nodes.resize(n + 1);
    for (i = 0; i < m; i++) {
        cin >> x >> y;
        nodes[x].from.push_back(y);
    }

    nodes[s].minim = 0;
    Recurs(s);
    for (i = 1; i <= n; i++)
        cout << nodes[i].minim << ' ';
}