Cod sursa(job #2565635)

Utilizator broaiemiupatriciu broaiemiu Data 2 martie 2020 15:39:15
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
// Lanterna.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <fstream>
#include <stdbool.h>
#define N 1000000
std::vector<int> muchii[2*N];
bool viz[N];
int scor[N], lst[2*N];
void bfs(int x, int dist) {
    viz[x] = true;
    int l=0;
    for (const auto& v : muchii[x]) {
        if (viz[v] == false) {
            scor[v] = dist + 1;
            lst[++l] = v;
        }
    }
    for (int i = 1; i <= l; i++) {
        bfs(lst[i], dist + 1);
    }
}
int main()
{
    //o introducere in bfs
    std::ifstream fin("bfs.in");
    std::ofstream fout("bfs.out");
    int n, m, k, a, b;
    fin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        fin >> a >> b;
        muchii[a].push_back(b);
        scor[i] = -1;
    }
    bfs(k,0);
    scor[k] = 0;
    for (int i = 1; i <= n; i++) {
        fout << scor[i] <<" ";
    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file