Cod sursa(job #2795617)

Utilizator Teodora11faranume Teodora11 Data 6 noiembrie 2021 18:22:02
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <vector>

#include "Algorithms.h"

using namespace std;

void solveDfs()
{
    int      N, M;
    ifstream in( "dfs.in" );
    in >> N >> M;
    vector<vector<int>> adjacencyList( N + 1 );

    int x, y;
    for( int i = 0; i < M; ++i ) {
        in >> x >> y;
        adjacencyList[x].push_back( y );
    }

    int          nrComponents = 0;
    vector<bool> visited( N + 1, false );
    ofstream     out( "dfs.out" );
    for( int startNode = 1; startNode <= N; ++startNode )
        if( !visited[startNode] ) {
            ++nrComponents;
            vector<int> traversal = Algorithms::DFS( adjacencyList, startNode, visited );
            /*out << "\nComponenta #" << nrComponents << ": ";
            for( auto node : traversal )
                out << node << ' ';*/
        }

    out << nrComponents;
}

int main()
{
    solveDfs();
}