Cod sursa(job #2650118)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 17 septembrie 2020 15:40:44
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#include <vector>
#define f in
#define g out

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

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 ( i=1; i <= m; i++ ){
        f>>x>>y;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    for ( i=1; i <= n; i++ )
        if ( !fr[i] ){
            cc++;
            dfs(i);
        }
    g<<cc;
    return 0;
}