Cod sursa(job #2422878)

Utilizator malina99oanea ana malina malina99 Data 20 mai 2019 10:24:22
Problema Parcurgere DFS - componente conexe Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <vector>
#include <fstream>
#include <iostream>

using namespace std;

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

vector <int> graph[100000];
int viz[100000], n, m;

void read() {
    f >> n >> m;
    for(int i = 0; i < m; i++) {
        int x, y;
        f >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
}

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

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

int noCompConex() {
    int i;
    int nrCom = 0;
    for( i = 1; i <= n; i++ )
        if( !viz[i] ) {
            nrCom++;
            dfs(i);
        }
    return nrCom;
}

using namespace std;
int main() {
    read();
    g << noCompConex();
    return 0;
}