Cod sursa(job #1824394)

Utilizator ShutterflyFilip A Shutterfly Data 7 decembrie 2016 20:01:00
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
using namespace std;

//TYPES DIVISION//
struct Entry {
    set<int> Neighbors;
    int Value;
};

//GLOBALS DIVISION//
int Load, Connections, Origin;
int Server, Client;
Entry Registry[100001];

//DFS//
void Executor(int _Target, int _Location, int _Value) {
    if (Registry[_Location].Value == 0)
        Registry[_Location].Value = _Value;

    for (auto _Client = Registry[_Location].Neighbors.begin(); _Client != Registry[_Location].Neighbors.end(); _Client++) {
        if (Registry[*_Client].Value == 0) {
            Registry[*_Client].Value = _Value;
            Executor(_Target, *_Client, _Value + 1);
        }
    }
}

//MAIN PROCEDURE//
int main() {
    freopen("bfs.in", "r", stdin);
    freopen("bfs.out", "w", stdout);
    cin >> Load >> Connections >> Origin;

    for (int i = 1; i <= Connections; i++) {
        cin >> Server >> Client;
        Registry[Server].Neighbors.insert(Client);
    }

    for (int i = 0; i <= Load; i++)
            Registry[i].Value = 0;

        Executor(1, Origin, 0);
        if (!Registry[1].Value)
            cout << "-1 ";
        else
            cout << Registry[1].Value << " ";

    for (int i = 2; i <= Load; i++) {
        for (int i = 0; i <= Load; i++)
            Registry[i].Value = 0;

        Executor(i, Origin, 0);
        if (!Registry[i].Value)
            cout << "-1 ";
        else
            cout << Registry[i].Value - 1 << " ";
    }
}