Cod sursa(job #2348389)

Utilizator malina99oanea ana malina malina99 Data 19 februarie 2019 17:51:19
Problema Parcurgere DFS - componente conexe Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream f("dfs.in");
ofstream g("dfs.out"); 

vector <int> graph[100000];
int viz[100000];

void DFS(int node) {
    int i;
    viz[node] = 1;
    
    int lim = graph[node].size();

    for( i = 0; i < lim; i++ )
        if( !viz[ graph[node][i] ] )
            DFS(graph[node][i]);
}

int main()
{
    
    int n, m;

    f >> n >> m;

    int i;
    for( i = 0; i < m; i++ ) {
        int x, y;
        f >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    
    int compConexe = 0;
    for( i = 1; i <= n; i++ ) {
        if( !viz[i] ) {
            compConexe++;
            DFS(i);
        }
    }
    
    g << compConexe;

    return 0;
}