Cod sursa(job #2498448)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 23 noiembrie 2019 22:09:01
Problema Parcurgere DFS - componente conexe Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define f in
#define g out

using namespace std;
ifstream in ( "dfs.in" );
ofstream out( "dfs.out" );
int n, m, i, x, y, cc;
vector<int> L[100009];
int fr[100009];

void dfs ( int nod ){
    fr[nod] = 1;
    for ( int i=0; i < L[nod].size(); i++ ){
        int vecin = L[nod][i];
        if ( !fr[vecin] )
            dfs( vecin );
    }
}


int main() {
    f>>n>>m;
    for ( ; m--; ){
        f>>x>>y;
        L[x].push_back(y);
    }
    for ( i=1; i <= n; i++ )
        if ( !fr[i] )
            cc++, dfs(i);
    g<<cc;
    return 0;
}